Добрый день! Вообщем создал форму, в которой при нажатии кнопки ищется номер из TextBox1 и относительно найденной ячейки вставляется текст из TextBox2. Проблема с поиском. Ищет только по листу.
[vba]
Код
Sub CommandButton1_Click() Dim WhatFind As String Dim sh As Worksheet, c As Range WhatFind = TextBox1 If TextBox1.TextLength = 6 Then For Each sh In Sheets Set c = sh.Cells.Find(What:=WhatFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False) Next If c Is Nothing Then MsgBox "Накладная не найдена" Else c.Parent.Activate c.Activate ActiveCell.Offset(0, 3).Value = TextBox2 End If Else MsgBox "Необходимо ввести 6-и значный номер накладной!", 0, "Ошибка" End If TextBox1.SetFocus TextBox1.SelStart = 0 TextBox1.SelLength = Len(TextBox1.Text) End Sub
[/vba]
Честно облазил все темы в этом разделе, видел посты про поиск, но не смог подобрать под свой код. Подскажите, пожалуйста Хочу еще добавить несколько функций, но без нормального поиска дело дальше не пойдет.
Добрый день! Вообщем создал форму, в которой при нажатии кнопки ищется номер из TextBox1 и относительно найденной ячейки вставляется текст из TextBox2. Проблема с поиском. Ищет только по листу.
[vba]
Код
Sub CommandButton1_Click() Dim WhatFind As String Dim sh As Worksheet, c As Range WhatFind = TextBox1 If TextBox1.TextLength = 6 Then For Each sh In Sheets Set c = sh.Cells.Find(What:=WhatFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False) Next If c Is Nothing Then MsgBox "Накладная не найдена" Else c.Parent.Activate c.Activate ActiveCell.Offset(0, 3).Value = TextBox2 End If Else MsgBox "Необходимо ввести 6-и значный номер накладной!", 0, "Ошибка" End If TextBox1.SetFocus TextBox1.SelStart = 0 TextBox1.SelLength = Len(TextBox1.Text) End Sub
[/vba]
Честно облазил все темы в этом разделе, видел посты про поиск, но не смог подобрать под свой код. Подскажите, пожалуйста Хочу еще добавить несколько функций, но без нормального поиска дело дальше не пойдет.Gladios74
Sub CommandButton1_Click() Dim WhatFind As String Dim sh As Worksheet, c As Range WhatFind = TextBox1 If TextBox1.TextLength = 6 Then For Each sh In Sheets Set c = sh.Cells.Find(What:=WhatFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False) If Not c Is Nothing Then c.Parent.Activate c.Activate ActiveCell.Offset(0, 3).Value = TextBox2 Exit For End If Next If c Is Nothing Then MsgBox "Накладная не найдена" End If Else MsgBox "Необходимо ввести 6-и значный номер накладной!", 0, "Ошибка" End If TextBox1.SetFocus TextBox1.SelStart = 0 TextBox1.SelLength = Len(TextBox1.Text) End Sub
[/vba]
Gladios74, здравствуйте, так? [vba]
Код
Sub CommandButton1_Click() Dim WhatFind As String Dim sh As Worksheet, c As Range WhatFind = TextBox1 If TextBox1.TextLength = 6 Then For Each sh In Sheets Set c = sh.Cells.Find(What:=WhatFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False) If Not c Is Nothing Then c.Parent.Activate c.Activate ActiveCell.Offset(0, 3).Value = TextBox2 Exit For End If Next If c Is Nothing Then MsgBox "Накладная не найдена" End If Else MsgBox "Необходимо ввести 6-и значный номер накладной!", 0, "Ошибка" End If TextBox1.SetFocus TextBox1.SelStart = 0 TextBox1.SelLength = Len(TextBox1.Text) End Sub
Sub CommandButton1_Click() Dim WhatFind As String Dim sh As Worksheet, c As Range WhatFind = TextBox1 If TextBox1.TextLength = 6 Then For i = 1 To Sheets.Count With Sheets(i).UsedRange Set c = Sheets(i).Cells.Find(What:=WhatFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False) If Not c Is Nothing Then FirstAddress = c.Address Do c.Parent.Activate c.Activate ActiveCell.Offset(0, 3).Value = TextBox2 Set c = .FindNext(c) Loop While c.Address <> FirstAddress End If End With Next Else MsgBox "Необходимо ввести 6-и значный номер накладной!", 0, "Ошибка" End If TextBox1.SetFocus TextBox1.SelStart = 0 TextBox1.SelLength = Len(TextBox1.Text) End Sub
[/vba]
Gladios74,
[vba]
Код
Sub CommandButton1_Click() Dim WhatFind As String Dim sh As Worksheet, c As Range WhatFind = TextBox1 If TextBox1.TextLength = 6 Then For i = 1 To Sheets.Count With Sheets(i).UsedRange Set c = Sheets(i).Cells.Find(What:=WhatFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False) If Not c Is Nothing Then FirstAddress = c.Address Do c.Parent.Activate c.Activate ActiveCell.Offset(0, 3).Value = TextBox2 Set c = .FindNext(c) Loop While c.Address <> FirstAddress End If End With Next Else MsgBox "Необходимо ввести 6-и значный номер накладной!", 0, "Ошибка" End If TextBox1.SetFocus TextBox1.SelStart = 0 TextBox1.SelLength = Len(TextBox1.Text) End Sub