I'm having issues with piece of code. We use the following to search a log for a specific information, populate a chart and then print and clear the chart once completed.
The thing is, if we change the search criteria from CIS to Inbound (or anything else for that matter) it refuses to populate the chart with the information from the log, but still prints out the chart headers.
This is the code we're using:
Private Sub cmdprint_Click()
Dim sdsheet As Worksheet, ersheet As Worksheet
Set sdsheet = Workbooks("HD Project.xls").Sheets("HelpdeskLogg")
Set ersheet = Workbooks("HD Project.xls").Sheets("report")
dlr = sdsheet.Cells(Rows.Count, 1).End(xlUp).Row
rlr = ersheet.Cells(Rows.Count, 1).End(xlUp).Row
y = 2
For x = 2 To dlr
If UCase(sdsheet.Cells(x, 6)) = "Inbound" And CDate(sdsheet.Cells(x, 3)) >= CDate(Me.txtdatestart) And CDate(sdsheet.Cells(x, 3)) <= CDate(Me.txtdateend) Then
ersheet.Cells(y, 1) = CDate(sdsheet.Cells(x, 3))
ersheet.Cells(y, 2) = sdsheet.Cells(x, 6)
ersheet.Cells(y, 3) = sdsheet.Cells(x, 7)
ersheet.Cells(y, 4) = sdsheet.Cells(x, 8)
ersheet.Cells(y, 5) = sdsheet.Cells(x, 9)
ersheet.Cells(y, 6) = sdsheet.Cells(x, 10)
ersheet.Cells(y, 7) = sdsheet.Cells(x, 11)
ersheet.Cells(y, 8) = sdsheet.Cells(x, 12)
ersheet.Cells(y, 9) = sdsheet.Cells(x, 13)
y = y + 1
'On Error Resume Next
End If
Next x
Dim Lastrow As Integer
Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Set printa = ersheet.Range("A1:i" & Lastrow)
printa.PrintOut
Sheets("report").Range("a2:i999").ClearContents
End Sub
Try changing:
UCase(sdsheet.Cells(x, 6)) = "Inbound" to
UCase(sdsheet.Cells(x, 6)) = "INBOUND"
Try changing:
UCase(sdsheet.Cells(x, 6)) = "Inbound" to
UCase(sdsheet.Cells(x, 6)) = "INBOUND"
This worked. Thank you for your help, barrleajo
Related
I have a userform that has a combo box at the top which will activate specific sheets based on the selection in the combobox. I have a search that works but it will only search and display data from sheet1 but I would like it to search and display data based on the active sheet. I have tried to update the code multiple different ways and place activesheet in place of sheet1 but it always errors out. If someone could assist with the code please let me know and I would appreciate it.
Private Sub cmdSearch_Click()
Dim totRows As Long, i As Long
totRows = Sheet1.range("A1").CurrentRegion.Rows.count
If txtname.Text = "" Then
MsgBox "Enter the name in the name block that you want to search"
End If
For i = 2 To totRows
If Trim(Sheet1.Cells(i, 1)) <> Trim(txtname.Text) And i = totRows Then
MsgBox "Name not found"
End If
If Trim(Sheet1.Cells(i, 1)) = Trim(txtname.Text) Then
txtname.Text = Sheet1.Cells(i, 1)
txtposition.Text = Sheet1.Cells(i, 2)
txtassigned.Text = Sheet1.Cells(i, 3)
cmbsection.Text = Sheet1.Cells(i, 4)
txtdate.Text = Sheet1.Cells(i, 5)
txtjoint.Text = Sheet1.Cells(i, 7)
txtDAS.Text = Sheet1.Cells(i, 8)
txtDEROS.Text = Sheet1.Cells(i, 9)
txtDOR.Text = Sheet1.Cells(i, 10)
txtTAFMSD.Text = Sheet1.Cells(i, 11)
txtDOS.Text = Sheet1.Cells(i, 12)
txtPAC.Text = Sheet1.Cells(i, 13)
ComboTSC.Text = Sheet1.Cells(i, 14)
txtTSC.Text = Sheet1.Cells(i, 15)
txtAEF.Text = Sheet1.Cells(i, 16)
txtPCC.Text = Sheet1.Cells(i, 17)
txtcourses.Text = Sheet1.Cells(i, 18)
txtseven.Text = Sheet1.Cells(i, 19)
txtcle.Text = Sheet1.Cells(i, 20)
txtnote.Text = Sheet1.Cells(i, 21)
Exit For
End If
Next i
End Sub
Combobox:
Private Sub ComboBox1_Change()
Dim actWsh As String
actWsh = ComboBox1.Text
Worksheets(actWsh).Select
End Sub
Comboboxbutton:
Private Sub CommandButton4_Click()
Me.ComboBox1.Clear
Dim strWs As String
Dim j As Integer
For j = 1 To ThisWorkbook.Sheets.count
Me.ComboBox1.AddItem Sheets(j).Name
Next
End Sub
Code that worked:
Private Sub cmdSearch_Click()
Dim wRow
If txtname.Text = "" Then
MsgBox "Enter the name in the name block that you want to search": Exit Sub
End If
With ActiveSheet
wRow = Application.Match(txtname.Text, .Columns(1), 0)
If Not IsError(wRow) Then
txtname.Text = .Cells(wRow, 1)
txtposition.Text = .Cells(wRow, 2)
txtassigned.Text = .Cells(wRow, 3)
cmbsection.Text = .Cells(wRow, 4)
txtdate.Text = .Cells(wRow, 5)
txtjoint.Text = .Cells(wRow, 7)
txtDAS.Text = .Cells(wRow, 8)
txtDEROS.Text = .Cells(wRow, 9)
txtDOR.Text = .Cells(wRow, 10)
txtTAFMSD.Text = .Cells(wRow, 11)
txtDOS.Text = .Cells(wRow, 12)
txtPAC.Text = .Cells(wRow, 13)
ComboTSC.Text = .Cells(wRow, 14)
txtTSC.Text = .Cells(wRow, 15)
txtAEF.Text = .Cells(wRow, 16)
txtPCC.Text = .Cells(wRow, 17)
txtcourses.Text = .Cells(wRow, 18)
txtseven.Text = .Cells(wRow, 19)
txtcle.Text = .Cells(wRow, 20)
txtnote.Text = .Cells(wRow, 21)
Else
MsgBox "Name not found"
End If
End With
End Sub
Step 1: On the topmost line of your code, add this line so that you may access this variable for later.
Dim actWsh As Workbook
Step 2: Replace your ComboBox1_Change() code with the one below. This will set the selected sheet to the variable actWsh
Private Sub ComboBox1_Change()
set actWsh = Worksheets(ComboBox1.Text)
actWsh.Activate
End Sub
Step 3: On your cmdSearch_Click() method, replace all Sheet1 with actWsh.
Hope this helps. :) Let me know if you have any other questions.
I am attempting to draw data from a separate sheet and put it into a corresponding cell if the conditions are met. My code works, but it is not efficient. I do not know how to change the For Next loop so that it attempts to draw data only until the final entry. Right now I have it set to go a hundred or so cells further than I need so that I wouldn't have to update the code as often when I input new data to the data sheet (or at least that was the thought). Here is my code:
Sub LRearTest()
Dim R As Integer
Dim j As Integer
For j = 89 To 250
For R = 1 To 300
If Worksheets("Input").Cells(j, 22).Value >= Worksheets("1036L").Cells(R, 5).Value And Worksheets("Input").Cells(j, 22).Value <= Worksheets("1036L").Cells(R, 6).Value Then
Worksheets("Input").Cells(j, 20).Value = Worksheets("1036L").Cells(R, 3).Value
End If
Next R
Next j
End Sub
The problem is when I run this code it takes almost two minutes before it is over. I am not sure if it is because I have used j and r as integers or what. Also I have a dozen of these on one module so I am not sure if that contributes. The code works like I said, it is just far too slow. Help is greatly appreciated.
The point that I am checking is in Column V of Sheet "Input". Each of my columns that I want to populate, F - U, use the same data in column V. The sheets that I am comparing the data in column V against are labeled as 1030L, 1030R, 1031L, 1031R, 1032L, 1032R, 1033L, 1033R, 1034L, 1034R, 1034LA, 1034RA, 1035L, 1035R, 1036L, and 1036R. The data being compared is in the same columns in every sheet. Thank you
Something like this should work for you:
Sub LRearTest()
Dim wb As Workbook
Dim wsInput As Worksheet
Dim wsData As Worksheet
Dim aDataParams() As String
Dim aInput As Variant
Dim aData As Variant
Dim InputIndex As Long
Dim DataIndex As Long
Dim ParamIndex As Long
Dim MinCol As Long
Set wb = ActiveWorkbook
Set wsInput = wb.Sheets("Input")
'Adjust the column associations for each sheet as necessary
ReDim aDataParams(1 To 16, 1 To 3)
aDataParams(1, 1) = "1030L": aDataParams(1, 2) = "F"
aDataParams(2, 1) = "1030R": aDataParams(2, 2) = "G"
aDataParams(3, 1) = "1031L": aDataParams(3, 2) = "H"
aDataParams(4, 1) = "1031R": aDataParams(4, 2) = "I"
aDataParams(5, 1) = "1032L": aDataParams(5, 2) = "J"
aDataParams(6, 1) = "1032R": aDataParams(6, 2) = "K"
aDataParams(7, 1) = "1033L": aDataParams(7, 2) = "L"
aDataParams(8, 1) = "1033R": aDataParams(8, 2) = "M"
aDataParams(9, 1) = "1034L": aDataParams(9, 2) = "N"
aDataParams(10, 1) = "1034R": aDataParams(10, 2) = "O"
aDataParams(11, 1) = "1034LA": aDataParams(11, 2) = "P"
aDataParams(12, 1) = "1034RA": aDataParams(12, 2) = "Q"
aDataParams(13, 1) = "1035L": aDataParams(13, 2) = "R"
aDataParams(14, 1) = "1035R": aDataParams(14, 2) = "S"
aDataParams(15, 1) = "1036L": aDataParams(15, 2) = "T"
aDataParams(16, 1) = "1036R": aDataParams(16, 2) = "U"
'Find minimum column
MinCol = wsInput.Columns.Count
For ParamIndex = LBound(aDataParams, 1) To UBound(aDataParams, 1)
If wsInput.Columns(aDataParams(ParamIndex, 2)).Column < MinCol Then MinCol = wsInput.Columns(aDataParams(ParamIndex, 2)).Column
Next ParamIndex
'Based on minimum column, determine column indexes for each sheet/column pair
For ParamIndex = LBound(aDataParams, 1) To UBound(aDataParams, 1)
aDataParams(ParamIndex, 3) = wsInput.Columns(aDataParams(ParamIndex, 2)).Column - MinCol + 1
Next ParamIndex
With wsInput.Range("F89", wsInput.Cells(wsInput.Rows.Count, "V").End(xlUp))
If .Row < 89 Then
MsgBox "No data in sheet [" & wsInput.Name & "]"
Exit Sub
End If
aInput = .Value
End With
For ParamIndex = LBound(aDataParams, 1) To UBound(aDataParams, 1)
'Define data sheet based on current column
Set wsData = wb.Sheets(aDataParams(ParamIndex, 1))
aData = wsData.Range("C1", wsData.Cells(wsData.Rows.Count, "F").End(xlUp)).Value
For InputIndex = LBound(aInput, 1) To UBound(aInput, 1)
For DataIndex = LBound(aData, 1) To UBound(aData, 1)
If aInput(InputIndex, UBound(aInput, 2)) >= aData(DataIndex, 3) _
And aInput(InputIndex, UBound(aInput, 2)) <= aData(DataIndex, 4) Then
aInput(InputIndex, aDataParams(ParamIndex, 3)) = aData(DataIndex, 1)
Exit For
End If
Next DataIndex
Next InputIndex
Set wsData = Nothing
Erase aData
Next ParamIndex
wsInput.Range("F89").Resize(UBound(aInput, 1), UBound(aInput, 2)) = aInput
Set wb = Nothing
Set wsInput = Nothing
Set wsData = Nothing
Erase aInput
Erase aData
Erase aDataParams
End Sub
Let me preface this with a huge thank you to anyone who takes the time to read this...
So I'm trying to make a simple 'Run Governance Report' button to pull data from one worksheet to another based on selected combobox variables in a Userform.
So far I have set up my two worksheets ("governance Reporting Data" and "Governance Report", a button "btnrun", and a userform "RunGovernance" set up.
What I cannot get to work is the following...
When the variables from the comboboxes are selected on the Userform, I cannot get it to populate data rows that only incorporate those variables...
I'd like to be able to select one or more of the variables from the combobox if possible and also like to put an "All" option in each of my comboboxes and have this grab All data available for that particular variable...
The code I have so far is as below;
Private Sub btnrun_Click()
Dim sdsheet As Worksheet, grsheet As Worksheet
Set sdsheet = ThisWorkbook.Sheets("Governance Reporting Data")
Set grsheet = ThisWorkbook.Sheets("Governance Report")
Dim match As Boolean
match = False
If sdsheet.Cells(Rows.Count, 4).End(xlUp).Row = 1 Then
sdlr = 2
Else
sdlr = sdsheet.Cells(Rows.Count, 4).End(xlUp).Row
End If
If grsheet.Cells(Rows.Count, 1).End(xlUp).Row = 1 Then
grlr = 2
Else
grlr = grsheet.Cells(Rows.Count, 1).End(xlUp).Row
End If
Me.Hide
'find selected data and populate in report spreadsheet
y = 2 ' starting row
'month
For x = 5 To sdlr
If sdsheet.Cells(x, 2) = Me.cmbmonth Then
'put on grsheet
grsheet.Cells(y, 1) = sdsheet.Cells(x, 3)
grsheet.Cells(y, 2) = sdsheet.Cells(x, 4)
grsheet.Cells(y, 3) = sdsheet.Cells(x, 5)
grsheet.Cells(y, 4) = sdsheet.Cells(x, 6)
grsheet.Cells(y, 5) = sdsheet.Cells(x, 7)
grsheet.Cells(y, 6) = sdsheet.Cells(x, 8)
grsheet.Cells(y, 7) = sdsheet.Cells(x, 9)
grsheet.Cells(y, 8) = sdsheet.Cells(x, 10)
grsheet.Cells(y, 9) = sdsheet.Cells(x, 11)
y = y + 1
Else
If sdsheet.Cells(x, 2) <> Me.cmbmonth Then
match = False
Exit For
End If
End If
'provider
If sdsheet.Cells(x, 4) = Me.cmbprovider Then
'put on grsheet
grsheet.Cells(y, 1) = sdsheet.Cells(x, 3)
grsheet.Cells(y, 2) = sdsheet.Cells(x, 4)
grsheet.Cells(y, 3) = sdsheet.Cells(x, 5)
grsheet.Cells(y, 4) = sdsheet.Cells(x, 6)
grsheet.Cells(y, 5) = sdsheet.Cells(x, 7)
grsheet.Cells(y, 6) = sdsheet.Cells(x, 8)
grsheet.Cells(y, 7) = sdsheet.Cells(x, 9)
grsheet.Cells(y, 8) = sdsheet.Cells(x, 10)
grsheet.Cells(y, 9) = sdsheet.Cells(x, 11)
y = y + 1
Else
If grsheet.Cells(x, 4) <> Me.cmbprovider Then
match = False
Exit For
End If
End If
'contract officer
If sdsheet.Cells(x, 5) = Me.cmbcontractofficer Then
'put on grsheet
grsheet.Cells(y, 1) = sdsheet.Cells(x, 3)
grsheet.Cells(y, 2) = sdsheet.Cells(x, 4)
grsheet.Cells(y, 3) = sdsheet.Cells(x, 5)
grsheet.Cells(y, 4) = sdsheet.Cells(x, 6)
grsheet.Cells(y, 5) = sdsheet.Cells(x, 7)
grsheet.Cells(y, 6) = sdsheet.Cells(x, 8)
grsheet.Cells(y, 7) = sdsheet.Cells(x, 9)
grsheet.Cells(y, 8) = sdsheet.Cells(x, 10)
grsheet.Cells(y, 9) = sdsheet.Cells(x, 11)
y = y + 1
Else
If grsheet.Cells(x, 5) <> Me.cmbcontractofficer Then
match = False
Exit For
End If
End If
'program
If sdsheet.Cells(x, 6) = Me.cmbprogram Then
'put on grsheet
grsheet.Cells(y, 1) = sdsheet.Cells(x, 3)
grsheet.Cells(y, 2) = sdsheet.Cells(x, 4)
grsheet.Cells(y, 3) = sdsheet.Cells(x, 5)
grsheet.Cells(y, 4) = sdsheet.Cells(x, 6)
grsheet.Cells(y, 5) = sdsheet.Cells(x, 7)
grsheet.Cells(y, 6) = sdsheet.Cells(x, 8)
grsheet.Cells(y, 7) = sdsheet.Cells(x, 9)
grsheet.Cells(y, 8) = sdsheet.Cells(x, 10)
grsheet.Cells(y, 9) = sdsheet.Cells(x, 11)
y = y + 1
Else
If grsheet.Cells(x, 6) <> Me.cmbprogram Then
match = False
Exit For
End If
End If
'issue
If sdsheet.Cells(x, 7) = Me.cmbissue Then
'put on grsheet
grsheet.Cells(y, 1) = sdsheet.Cells(x, 3)
grsheet.Cells(y, 2) = sdsheet.Cells(x, 4)
grsheet.Cells(y, 3) = sdsheet.Cells(x, 5)
grsheet.Cells(y, 4) = sdsheet.Cells(x, 6)
grsheet.Cells(y, 5) = sdsheet.Cells(x, 7)
grsheet.Cells(y, 6) = sdsheet.Cells(x, 8)
grsheet.Cells(y, 7) = sdsheet.Cells(x, 9)
grsheet.Cells(y, 8) = sdsheet.Cells(x, 10)
grsheet.Cells(y, 9) = sdsheet.Cells(x, 11)
y = y + 1
Else
If grsheet.Cells(x, 7) <> Me.cmbissue Then
match = False
Exit For
End If
End If
'status
If sdsheet.Cells(x, 11) = Me.cmbstatus Then
'put on grsheet
grsheet.Cells(y, 1) = sdsheet.Cells(x, 3)
grsheet.Cells(y, 2) = sdsheet.Cells(x, 4)
grsheet.Cells(y, 3) = sdsheet.Cells(x, 5)
grsheet.Cells(y, 4) = sdsheet.Cells(x, 6)
grsheet.Cells(y, 5) = sdsheet.Cells(x, 7)
grsheet.Cells(y, 6) = sdsheet.Cells(x, 8)
grsheet.Cells(y, 7) = sdsheet.Cells(x, 9)
grsheet.Cells(y, 8) = sdsheet.Cells(x, 10)
grsheet.Cells(y, 9) = sdsheet.Cells(x, 11)
y = y + 1
Else
If grsheet.Cells(x, 11) <> Me.cmbstatus Then
match = False
Exit For
End If
End If
Next
'jump to report
grsheet.Visible = True
grsheet.Select
'print preview option
If Me.cbprintpreview = True Then
grsheet.PrintPreview
End If
'close report
answer = MsgBox("Would you like to close this report?", vbYesNo, "Close Report?")
If answer = vbYes Then
grsheet.Visible = False
'clear last report
grsheet.Range("A2:i" & grlr).ClearContents
End If
End Sub
Untested. Assumes all your comboboxes have an "All" option:
Private Sub btnrun_Click()
Dim sdsheet As Worksheet, grsheet As Worksheet
Dim sdlr As Long, grlr As Long, y As Long, x As Long
Set sdsheet = ThisWorkbook.Sheets("Governance Reporting Data")
Set grsheet = ThisWorkbook.Sheets("Governance Report")
Dim match As Boolean
match = False
sdlr = Application.Max(sdsheet.Cells(Rows.Count, 4).End(xlUp).Row, 2)
'## are you sure you want to get this here?
grlr = Application.Max(grsheet.Cells(Rows.Count, 1).End(xlUp).Row, 2)
y = 2 ' starting row << not grlr ?
'month
For x = 5 To sdlr
If Me.cmbmonth = "All" Or sdsheet.Cells(x, 2) = Me.cmbmonth Then
If Me.cmbprovider = "All" Or sdsheet.Cells(x, 4) = Me.cmbprovider Then
If Me.cmbcontractofficer = "All" Or sdsheet.Cells(x, 5) = Me.cmbcontractofficer Then
If Me.cmbprogram = "All" Or sdsheet.Cells(x, 6) = Me.cmbprogram Then
If Me.cmbissue = "All" Or sdsheet.Cells(x, 7) = Me.cmbissue Then
If Me.cmbstatus = "All" Or sdsheet.Cells(x, 11) = Me.cmbstatus Then
grsheet.Cells(y, 1).Resize(1, 9).Value = sdsheet.Cells(x, 3).Resize(1, 9).Value
y = y + 1
match = True
End If
End If
End If
End If
End If
End If
Next
grsheet.Visible = True
grsheet.Activate
If Me.cbprintpreview = True Then grsheet.PrintPreview
If MsgBox("Would you like to close this report?", vbYesNo, "Close Report?") = vbYes Then
grsheet.Visible = False
grsheet.Range("A2:I" & grlr).ClearContents '<< grlr value will not be current ?
End If
End Sub
I cannot seem to solve this VBA riddle I've been working on, please help. I'm new at this and I'm probably over complicating it
Essentially, there are two worksheets - one titled Master and the other will be created fresh daily by date. The Master tab contains 10000 rows of historical data filled from Columns A:X. The other tab generally has about 300 rows of fresh data and also contains like Columns A:X, only with blank cells in Columns A:B. I'm trying to find matches with the master tab, and if so, populate the corresponding results in cells A and B from the master to the daily. If nothing, leave blank. It is crucial that Cells H:M and R:W are identical matches.
Below is my crazy attempt, Thank you in advance for helping
Sub Previous()
Dim u As Long
u = 2
Do While ActiveSheet.Cells(u, 6) <> ""
Dim i As Long
i = 2
Do While Worksheets("Master").Cells(i, 6) <> ""
If ActiveSheet.Range(Cells(u, 8), Cells(u, 13)) _
= Worksheets("Master").Range(Cells(i, 8), Cells(i, 13)) _
And ActiveSheet.Range(Cells(u, 18), Cells(u, 23)) _
= Worksheets("Master").Range(Cells(i, 18), Cells(i, 23)) _
And ActiveSheet.Cells(u, 2) = "" Then
ActiveSheet.Range(Cells(u, 1), Cells(u, 2)) _
= Worksheets("Master").Range(Cells(i, 1), Cells(i, 2))
Else: i = i + 1
End If
Loop
u = u + 1
i = 2
Loop
End Sub
First of all, I don't believe this snippet does what you think it does.
Worksheets("Master").Range(Cells(i, 8), Cells(i, 13))
In that snippet Cells(i,8) references the ActiveSheet, not Sheets("Master").
There is a note on this about halfway down the page on msdn's Range Object documentation.
You can simplify your code a great deal by assigning some worksheet variables.
dim actWs as Worksheet
dim mstWs as Worksheet
Set actWs = Activesheet
Set mstWs = Sheets("Master")
'then reference your ranges like this
mstWs.Cells(i,8)
But, that's not what is causing your runtime error.
Simply put, you can not compare ranges that way. You need to check the value of each cell, so you end up with another layer of nested loops.
dim u as long ' active sheet row counter
dim i as long ' master sheet row counter
dim c as long ' column counter
For u = 2 to actWs.Range("A" & .Rows.Count).End(xlUp).Row 'find last row in column "A" of active sheet
For i = 2 to mstWs.Range("A" & .Rows.Count).End(xlUp).Row 'find last row in column "A" of master sheet
For c = 8 to 13
If actWs.Cells(i,c) = mstWs.Cells(i,c) Then
'Do stuff
End if
next c 'next column
next i 'next master sheet row
next u 'next active sheet row
This is obviously a simplified version of what you'll need to do. Be careful of line continuations (" _ ") and code indentation. It's easy to trick yourself into thinking your program should flow in a way that it isn't. It would be advisable to store the value's you're checking for equality in variables to make it easier to read. You might more readily notice where you're going wrong.
Sub Previous()
Dim actWs As Worksheet
Set actWs = ActiveSheet
Dim mstWs As Worksheet
Set mstWs = Sheets("Master")
Dim u As Long
Dim i As Long
u = 2
Do While actWs.Cells(u, 6) <> ""
For i = 2 To mstWs.Range("C" & Rows.Count).End(xlUp).Row
If actWs.Cells(u, 8) = mstWs.Cells(i, 8) And actWs.Cells(u, 9) = mstWs.Cells(i, 9) And actWs.Cells(u, 10) = mstWs.Cells(i, 10) And actWs.Cells(u, 11) = mstWs.Cells(i, 11) And actWs.Cells(u, 12) = mstWs.Cells(i, 12) And actWs.Cells(u, 13) = mstWs.Cells(i, 13) And actWs.Cells(u, 18) = mstWs.Cells(i, 18) And actWs.Cells(u, 19) = mstWs.Cells(i, 19) And actWs.Cells(u, 20) = mstWs.Cells(i, 20) And actWs.Cells(u, 21) = mstWs.Cells(i, 21) And actWs.Cells(u, 22) = mstWs.Cells(i, 22) And actWs.Cells(u, 23) = mstWs.Cells(i, 23) Then
mstWs.Select
Range(Cells(i, 1), Cells(i, 2)).Select
Selection.Copy
actWs.Select
Range(Cells(u, 1), Cells(u, 2)).Select
actWs.Paste
End If
Next i
u = u + 1
Loop
End Sub
I want to be able to add multiple data in 2 sheets. With that I have an error:
Run Time Error '91' : Object Variable or With Block not Set
Referring to this line:
With Sheetclient = ThisWorkbook.Sheets(CMB_Test.Value)
The first page is chosen by a combobox value and it's working well, and the second page will automatically something the page: "testbit".
Private Sub Save_test_Click()
Dim Sheetclient As Worksheet
Dim testbit1 As Worksheet
Dim nr As Integer, lr As Integer
With Sheetclient = ThisWorkbook.Sheets(CMB_Test.Value)
nr = Sheetclient.Cells(Rows.Count, 1).End(xlUp).Row + 1
Sheetclient.Cells(nr, 5) = Me.TB_dateBit.Value
Sheetclient.Cells(nr, 6) = Me.serial.Value
Sheetclient.Cells(nr, 7) = Me.matrice.Value
Sheetclient.Cells(nr, 8) = Me.CMB_config.Value
Sheetclient.Cells(nr, 9) = Me.lifetime.Value
End With
With testbit1 = ThisWorkbook.Sheets("testbit")
nr = testbit1.Cells(Rows.Count, 1).End(xlUp).Row + 1
testbit1.Cells(nr, 1) = Me.TB_dateBit.Value
testbit1.Cells(nr, 2) = Me.serial.Value
testbit1.Cells(nr, 3) = Me.matrice.Value
testbit1.Cells(nr, 4) = Me.CMB_config.Value
testbit1.Cells(nr, 5) = Me.lifetime.Value
End with
End
End Sub
You need to Set the object - and having Set it, you can use . to reference it. Thus your code might look like this:
Set Sheetclient = ThisWorkbook.Sheets(CMB_Test.Value)
With Sheetclient
nr = .Cells(Rows.Count, 1).End(xlUp).Row + 1
.Cells(nr, 5) = Me.TB_dateBit.Value
.Cells(nr, 6) = Me.serial.Value
.Cells(nr, 7) = Me.matrice.Value
.Cells(nr, 8) = Me.CMB_config.Value
.Cells(nr, 9) = Me.lifetime.Value
End With
Same for the second part of the code