object variable with block when try select sheet from combobox on userform

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
354
Office Version
  1. 2016
Platform
  1. Windows
Hello
I try copying data from userform based on combobox to selected sheet but it gives error when press commandbutton1
object variable or with block in this line
VBA Code:
Set myrange = sh.Columns(2).Find(ComboBox1, LookIn:=xlValues, lookat:=xlWhole)
the whole code
VBA Code:
Private Sub CommandButton1_Click()
Dim Name As String
Dim sh As Worksheet
Dim myrange As Range
Dim i As Long
For i = 2 To Sheets.Count
Me.ComboBox1.AddItem Sheets(i).Name
If Me.ComboBox1.Value = "" And Me.ComboBox1.ListIndex = -1 Then Exit Sub
If Me.ComboBox1.Value <> "" Then
Set myrange = sh.Columns(2).Find(ComboBox1, LookIn:=xlValues, lookat:=xlWhole)
If Not myrange Is Nothing Then
With myrange
 .Offset(, 1) = TextBox1.Value
.Offset(, 2) = TextBox2.Value
End With
End If
End If
Next
End Sub

VBA Code:
Private Sub UserForm_Initialize()
Dim i As Long
For i = 2 To Sheets.Count
Me.ComboBox1.AddItem Sheets(i).Name
Next i
End Sub
any help guys,please?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
You declare the variable sh as a worksheet object but you didn't SET it to anything before you try to use it.
 
Upvote 0
Hi,
do you mean like this
VBA Code:
Set sh = Worksheets(i)
if it's so this doesn't work "error subscript ou of range "
if it's wrong , what the right,please?
 
Upvote 0
@Alex Blakenburg thanks for correct me , but it doesn't copy to the sheet when fill textboxes . the header start from row 4 , should copy start from row5 .
 
Upvote 0
I don't have visibility of your worksheet.
If your find it looking for the heading, then myrange is your heading cell & row in Column 2.
With your code below you are going first 1 column to the right and then 2 columns to the right.
If this is means to be 1 row down then 2 rows down just remove the comma ","

Rich (BB code):
With myrange
 .Offset(, 1) = TextBox1.Value
.Offset(, 2) = TextBox2.Value
End With
 
Upvote 0
code seems to need more arranging .
the structure of data for each sheet like this
test.xlsm
BCD
4CODENAMEPLACE
5ATT-100
6ATT-101
7ATT-102
8ATT-103
9ATT-104
10ATT-105
11ATT-106
12ATT-107
13ATT-108
14ATT-109
15ATT-110
16ATT-111
17ATT-112
18ATT-113
sep


should match item in column B for specific sheet is selected from combobox1 and when fill textbox1,2 then should fill next adjacent cells
like this
test.xlsm
BCD
4CODENAMEPLACE
5ATT-100kal1ppl1
6ATT-101
7ATT-102
8ATT-103
9ATT-104
10ATT-105
11ATT-106
12ATT-107
13ATT-108
14ATT-109
15ATT-110
16ATT-111
17ATT-112
18ATT-113
sep
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,109
Members
452,302
Latest member
TaMere

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top