Trying to delete empty rows in VBA with command button - excel

i'm trying to solve a problem of mine and i'm pretty much a newbie in VBA.
I'm trying to make a quotation out of excel utilizing user form.
Transferring data is doable from the user form, but am having some difficulty to complete the quotation:
1. Creating new empty row base on input in user form
2. assign input to different rows and deleting any empty rows if there are no input.
This is my userform:
Private Sub okaybutton_Click()
'Make quotation activate
Sheet11.Activate
'Trasnfer Information sheet
Cells(2, 6).Value = DateBox.Value
Cells(6, 2).Value = "Company: " + CompanyBox.Value
Cells(8, 2).Value = "State: " + StateBox.Value
Cells(9, 2).Value = "Name: " + PICBox.Value
Cells(10, 2).Value = "Contact Number: " + ContactCustomer.Value
Cells(7, 2).Value = "Address: " + AddressBox.Value
Cells(7, 6).Value = SEBox.Value
Cells(8, 6).Value = CNBox.Value
Cells(11, 2).Value = CusEmail.Value
Cells(9, 6).Value = ACemail.Value
If PTWrequire.Value = True Then
Cells(13, 2).Value = "PTW application or safety induction required at site"
End If
If ESDrequire.Value = True Then
Cells(13, 2).Value = Cells(13, 2).Value & " " & " & ESD Attire required."
End If
'SupplySide information sheet
'Determine emptyRow
nextrow = WorksheetFunction.CountA(Range("B:B"))
nextrow1 = WorksheetFunction.CountA(Range("B:B")) + 1
nextrow2 = WorksheetFunction.CountA(Range("B:B")) + 2
'Dim nextrow As Long
'nextrow = Cells(Rows.Count, "A").End(xlUp).Row + 1
'flow measurement point 1
If FlowMeasure1.Value = True Then
Cells(nextrow, 3).Value = "Flow measurement, Measures dry air flow capacity."
If Hottap1.Value = "Yes" Then
Cells(nextrow, 3).Value = Cells(nextrow, 3).Value & "- perform hot tapping on " & "Main header size: " & Pipesize1.Value & """."
Else
Cells(nextrow, 3).Value = Cells(nextrow, 3).Value & " Main header size: " & Pipesize1.Value & """."
End If
If Pipesize1.Value = 2 Then
Cells(nextrow, 4).Value = "3700"
ElseIf Pipesize1.Value = 2.5 Then
Cells(nextrow, 4).Value = "3706"
ElseIf Pipesize1.Value = 3 Then
Cells(nextrow, 4).Value = "3945"
ElseIf Pipesize1.Value = 4 Then
Cells(nextrow, 4).Value = "3971"
ElseIf Pipesize1.Value = 5 Then
Cells(nextrow, 4).Value = "3971"
ElseIf Pipesize1.Value = 6 Then
Cells(nextrow, 4).Value = "4080"
End If
If SSquantity1.Value > 0 Then
Cells(nextrow, 2).Value = SSquantity1.Value
End If
'flow measurement point 2
If Hottap2.Value = "Yes" Then
Cells(nextrow1, 3).Value = "Flow measurement, Measures dry air flow capacity." & "- perform hot tapping on " & "Main header size: " & Pipesize2.Value & """."
ElseIf Hottap2.Value = "No" Then
Cells(nextrow1, 3).Value = Cells(nextrow1, 3).Value & " Main header size: " & Pipesize2.Value & """."
End If
If Pipesize2.Value = 2 Then
Cells(nextrow1, 4).Value = "3700"
ElseIf Pipesize2.Value = 2.5 Then
Cells(nextrow1, 4).Value = "3706"
ElseIf Pipesize2.Value = 3 Then
Cells(nextrow1, 4).Value = "3945"
ElseIf Pipesize2.Value = 4 Then
Cells(nextrow1, 4).Value = "3971"
ElseIf Pipesize2.Value = 5 Then
Cells(nextrow1, 4).Value = "3971"
ElseIf Pipesize2.Value = 6 Then
Cells(nextrow1, 4).Value = "4080"
End If
If SSquantity2.Value > 0 Then
Cells(nextrow1, 2).Value = SSquantity2.Value
End If
'flow measurement point 3
If Hottap3.Value = "Yes" Then
Cells(nextrow2, 3).Value = "Flow measurement, Measures dry air flow capacity." & "- perform hot tapping on " & "Main header size: " & Pipesize3.Value & """."
ElseIf Hottap3.Value = "No" Then
Cells(nextrow2, 3).Value = Cells(nextrow2, 3).Value & " Main header size: " & Pipesize3.Value & """."
End If
If Pipesize3.Value = 2 Then
Cells(nextrow2, 4).Value = "3700"
ElseIf Pipesize3.Value = 2.5 Then
Cells(nextrow2, 4).Value = "3706"
ElseIf Pipesize3.Value = 3 Then
Cells(nextrow2, 4).Value = "3945"
ElseIf Pipesize3.Value = 4 Then
Cells(nextrow2, 4).Value = "3971"
ElseIf Pipesize3.Value = 5 Then
Cells(nextrow2, 4).Value = "3971"
ElseIf Pipesize3.Value = 6 Then
Cells(nextrow2, 4).Value = "4080"
End If
If SSquantity3.Value > 0 Then
Cells(nextrow2, 2).Value = SSquantity3.Value
End If
End If
On Error Resume Next
Worksheet.Columns("B:B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
End Sub

Related

Excel VBA bug query

I currently am using a userform I created to log data into a spreadsheet. During data validation, I notice that when I key in dates in the DD/MM/YYYY format in the userform, some rows swap the DD/MM to MM/DD, which causes confusion downstream.
I adjusted the data type for the entire column, but the userform code seems act differently for the particular row. Is this a bug or am I overlooking a line of code somewhere?
Here are the images of the userform and the data in the spreadsheet, as well as the code for the information transfer segment.
Data from spreadsheet
Userform Date segment
Code for transfer information:
'Transfer information
Cells(emptyRow, 1).Value = p
Cells(emptyRow, 3).Value = hour.Value & ":" & minute.Value & " " & ampm.Value
Cells(emptyRow, 4).Value = PTID.Value
Cells(emptyRow, 2).Value = cmbdate.Value & "/" & cmbmonth.Value & "/" & cmbyear.Value
Cells(emptyRow, 5).Value = UNIT.Value
Cells(emptyRow, 6).Value = PCBOX.Value
Cells(emptyRow, 7).Value = WASTE.Value
Cells(emptyRow, 8).Value = REPORTED.Value
Cells(emptyRow, 9).Value = DETBOX.Value
Cells(emptyRow, 10).Value = FOLBOX.Value
Cells(emptyRow, 11).Value = SUMBOX.Value
Cells(emptyRow, 12).Value = CAPBOX.Value
Cells(emptyRow, 13).Value = EHOR.Value
Cells(emptyRow, 14).Value = TECHS.Value & "," & TECHS2.Value & "," & TECHS3.Value & "," & TECHS4.Value
Cells(emptyRow, 15).Value = ERRORBOX.Value
Cells(emptyRow, 16).Value = PREVBOX.Value
Cells(emptyRow, 17).Value = SOP.Value
Cells(emptyRow, 18).Value = AUDIFILE.Value
Cells(emptyRow, 19).Value = INTERFILE.Value
Cells(emptyRow, 20).Value = cmbdate2.Value & "/" & cmbmonth2.Value & "/" & cmbyear2.Value
Cells(emptyRow, 23).Value = Phase.Value
Cells(emptyRow, 24).Value = QIM.Value
MsgBox "Please check your entry in the sheet", , "Entry Complete"
MsgBox "Your entry serial number is " & p
SN.Text = p
VNCFORM.Hide
Code for recall information (to same userform, when a serial number for the entry is entered into the userform)
Private Sub SN_AfterUpdate()
'TO RETRIEVE S/N DATA TO THE USERFORM'
Dim x As Range
Dim y As Long
Set WS = Worksheets("Data")
y = Application.WorksheetFunction.Match(CLng(Me.SN.Value), WS.Range("A:A"), 0)
'POSSIBLE PROBLEM AREA'
Me.cmbdate.Value = Left(WS.Range("B" & y).Value, 2)
Me.cmbmonth.Value = Mid(WS.Range("B" & y).Value, 4, 2)
Me.cmbyear.Value = Right(WS.Range("B" & y).Value, 4)
Me.hour.Value = CStr(Left(WS.Range("C" & y).Value, 2))
Me.minute.Value = CStr(Mid(WS.Range("C" & y).Value, 4, 2))
Me.ampm.Value = CStr(Right(WS.Range("C" & y).Value, 2))
Me.PTID.Value = WS.Range("D" & y).Value
Me.UNIT.Value = WS.Range("E" & y).Value
Me.PCBOX.Value = WS.Range("F" & y).Value
Me.WASTE.Value = WS.Range("G" & y).Value
Me.REPORTED.Value = WS.Range("H" & y).Value
Me.DETBOX.Value = WS.Range("I" & y).Value
Me.FOLBOX.Value = WS.Range("J" & y).Value
Me.SUMBOX.Value = WS.Range("K" & y).Value
Me.CAPBOX.Value = WS.Range("L" & y).Value
Me.EHOR.Value = WS.Range("M" & y).Value
'Techs involved in case transcribed back to userform
Dim MYARRAY() As String, MYSTRING As String
MYSTRING = WS.Range("N" & y).Value
MYARRAY = Split(MYSTRING, ",")
For N = 0 To UBound(MYARRAY)
Me.TECHS.Value = MYARRAY(0)
Me.TECHS2.Value = MYARRAY(1)
Me.TECHS3.Value = MYARRAY(2)
Me.TECHS4.Value = MYARRAY(3)
Next N
Me.ERRORBOX.Value = WS.Range("O" & y).Value
Me.PREVBOX.Value = WS.Range("P" & y).Value
Me.SOP.Value = WS.Range("Q" & y).Value
Me.AUDIFILE.Value = WS.Range("R" & y).Value
Me.INTERFILE.Value = WS.Range("S" & y).Value
Me.cmbdate2.Value = Left(WS.Range("T" & y).Value, 2)
Me.cmbmonth2.Value = Mid(WS.Range("T" & y).Value, 4, 2)
Me.cmbyear2.Value = Right(WS.Range("T" & y).Value, 4)
Me.Phase.Value = WS.Range("W" & y).Value
Me.QIM.Value = WS.Range("X" & y).Value
End Sub
The problem seems to occur when I recall data back into the userform where the month value and the date values get swapped for some reason.
Is there a property of code I am overlooking? Or could I improve the code somehow; I think the error comes from the recall segment (see: 'POSSIBLE PROBLEM AREA')
I would make sure when writing/reading dates you're being more explicit:
'write date to sheet
Cells(emptyRow, 2).Value = DateSerial(CLng(cmbyear.Value), _
CLng(cmbmonth.Value), _
CLng(cmbdate.Value))
'read date from sheet
Dim dt As Date
dt = WS.Range("B" & y).Value
Me.cmbdate.Value = Day(dt)
Me.cmbmonth.Value = Month(dt)
Me.cmbyear.Value = Year(dt)
Also likely need some code to check there's an entry before trying to write/read it.

Editing Excel from Access VBA Object Required Error

I have a program that opens an Excel spreadsheet and makes changes to it. I am always editing the first sheet but if it is a certain type of report I want to edit the second sheet as well. This all works fine for me on the first sheet and all but centering the text in the cell on the second sheet. I get an Object Required error only when I try to do this. I center the text in the cells on the first sheet no problem. The error only comes after I pass the object to the new procedure. Here is some of my code:
First Procedure
Private Sub OSummary1(strfile As String, strTableResults As String, dtUnivDt As Date)
Dim xlApp As Object
Dim objWorkbook As Object
Dim objSheet As Object
Set xlApp = CreateObject("Excel.Application")
Set objWorkbook = xlApp.Workbooks.Open(strfile)
Set objSheet = objWorkbook.Sheets(1)
Later in the code
ElseIf Mid(strTableResults, 11, 1) = 1 Then
Max = 11
Do Until i > Max
If .Cells(i, 4).Value = "0" And .Cells(i, 2).Value = "0" And .Cells(i, 3).Value = "0" Then
.Cells(i, 4).Value = "NA"
.Cells(i, 4).Interior.ColorIndex = 15
.Cells(i, 3).Value = "-"
.Cells(i, 2).Value = "-"
ElseIf .Cells(i, 2).Value = "0" Then
.Cells(i, 4).Value = "0.0"
.Cells(i, 4).Interior.ColorIndex = 22
ElseIf .Cells(i, 4).Value >= "95.00" Or .Cells(i, 4).Value = "100" Then
.Cells(i, 4).Interior.ColorIndex = 43
ElseIf .Cells(i, 4).Value >= "90.00" And .Cells(i, 4).Value < "95.00" Then
.Cells(i, 4).Interior.ColorIndex = 36
Else
.Cells(i, 4).Interior.ColorIndex = 22
End If
If .Cells(i, 4).Value = 0 Then
.Cells(i, 4).NumberFormat = "0.00%"
ElseIf Not .Cells(i, 4).Value Like "*.*" Then
.Cells(i, 4).NumberFormat = "#.00""%"""
ElseIf .Cells(i, 4).Value Like "*.#" Then
.Cells(i, 4).NumberFormat = "#.#0""%"""
Else
.Cells(i, 4).NumberFormat = "#.##""%"""
End If
If .Cells(i, 1).Value = "AppealNotificationTimeliness" Then
.Cells(i, 1).Font.Bold = True
.Cells(i, 2).Font.Bold = True
.Cells(i, 3).Font.Bold = True
.Cells(i, 4).Font.Bold = True
.Cells(i, 2).HorizontalAlignment = xlCenter
.Cells(i, 3).HorizontalAlignment = xlCenter
.Cells(i, 4).HorizontalAlignment = xlCenter
iB = Len(.Cells(i, 2).Value)
iC = Len(.Cells(i, 3).Value)
iD = .Cells(i, 4).Value
Else
'Indent header
.Cells(i, 1).IndentLevel = 3
'Indent sub-headers
If iB < 3 Then
.Cells(i, 2).IndentLevel = 5
ElseIf iB > 2 And iB < 5 Then
.Cells(i, 2).IndentLevel = 4
ElseIf iB > 4 And iB < 7 Then
.Cells(i, 2).IndentLevel = 3
Else
.Cells(i, 2).IndentLevel = 2
End If
If iC < 3 Then
.Cells(i, 3).IndentLevel = 4
ElseIf iC > 2 And iC < 5 Then
.Cells(i, 3).IndentLevel = 3
ElseIf iC > 4 And iC < 7 Then
.Cells(i, 3).IndentLevel = 2
Else
.Cells(i, 3).IndentLevel = 1
End If
If iD = "NA" Then
.Cells(i, 4).IndentLevel = 5
ElseIf iD = "100" Then
.Cells(i, 4).IndentLevel = 3
Else
.Cells(i, 4).IndentLevel = 4
End If
End If
i = i + 1
Loop
If Right(strTableResults, 3) = "FDR" Then
Call FDRTable1(objWorkbook)
End If
This all works fine for sheet 1
Second Procedure from Call above
Private Sub FDRTable1(ByRef objWorkbook As Object)
Dim objSheet As Object
Dim RowCnt As Integer
Dim CurrentRow As Integer
Dim CurrentRowVal As String
Dim iRange As Range
Dim iCells As Range
Dim i As Integer
Dim Max As Integer
Set objSheet = objWorkbook.Sheets(2)
i = 2
With objSheet
'Header
.Cells(1, 1).Font.Size = 12
.Range("A1:G1").Font.Bold = True
.Cells.EntireColumn.AutoFit
.Range("A2:G6").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
.Range("A7:G9").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
.Range("A10:G14").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
.Range("A15:G17").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
Max = 17
Do Until i > Max
If .Cells(i, 7).Value = "0" And .Cells(i, 4).Value = "0" And .Cells(i, 6).Value = "0" Then
.Cells(i, 7).Value = "NA"
.Cells(i, 7).Interior.ColorIndex = 15
.Cells(i, 4).Value = "-"
.Cells(i, 5).Value = "-"
.Cells(i, 6).Value = "-"
ElseIf .Cells(i, 4).Value = "0" Then
.Cells(i, 7).Value = "0.0"
.Cells(i, 7).Interior.ColorIndex = 22
ElseIf .Cells(i, 7).Value >= "95.00" Or .Cells(i, 7).Value = "100" Then
.Cells(i, 7).Interior.ColorIndex = 43
ElseIf .Cells(i, 7).Value >= "90.00" And .Cells(i, 7).Value < "95.00" Then
.Cells(i, 7).Interior.ColorIndex = 36
Else
.Cells(i, 7).Interior.ColorIndex = 22
End If
If .Cells(i, 7).Value = 0 Then
.Cells(i, 7).NumberFormat = "0.00%"
.Cells(i, 4).HorizontalAlignment.xlCenter
.Cells(i, 5).HorizontalAlignment.xlCenter
.Cells(i, 6).HorizontalAlignment.xlCenter
.Cells(i, 7).HorizontalAlignment.xlCenter
ElseIf Not .Cells(i, 7).Value Like "*.*" Then
.Cells(i, 7).NumberFormat = "#.00""%"""
.Cells(i, 4).HorizontalAlignment.xlCenter
.Cells(i, 5).HorizontalAlignment.xlCenter
.Cells(i, 6).HorizontalAlignment.xlCenter
.Cells(i, 7).HorizontalAlignment.xlCenter
ElseIf .Cells(i, 7).Value Like "*.#" Then
.Cells(i, 7).NumberFormat = "#.#0""%"""
.Cells(i, 4).HorizontalAlignment.xlCenter
.Cells(i, 5).HorizontalAlignment.xlCenter
.Cells(i, 6).HorizontalAlignment.xlCenter
.Cells(i, 7).HorizontalAlignment.xlCenter
Else
.Cells(i, 7).NumberFormat = "#.##""%"""
.Cells(i, 4).HorizontalAlignment.xlCenter
.Cells(i, 5).HorizontalAlignment.xlCenter
.Cells(i, 6).HorizontalAlignment.xlCenter
.Cells(i, 7).HorizontalAlignment.xlCenter
End If
i = i + 1
Loop
End With
End Sub
This all works too except I get the error when I try to center the text (.HorizontalAlignment.xlCenter). If I remove those lines, it works fine.

VBA Excel If statement with AND/OR function

Just want to know your idea on this matter. So the thing is I'm trying to show the information in the UserForm with the MultiPage control. I have a Cert sheet where the data are stored. So in the Cert sheet, the count of the data can be equals to 1, 2, or 3. I already figured out the flow for 1 and 3 but I'm having a problem with the 2.
My plan is like this:
If the data = 2 then
If box = 1 or box = 2 Then
Show data in first and second box
If box = 1 or box = 3 Then
Show data in first and third box
If box = 2 or box = 3 Then
Show data in second and third box
Here's a visual for your guide.
USERFORM: If the data is equal to 2 or 3, data should be shown in their respective boxes.
SHEET DATA:
The code below is the one that I'm currently using for 2.
For r = 9 To Lastrow
If Application.CountIf(Worksheets(ws_output).Columns(3), LRN) = 2 Then 'data
If _
ws.Cells(r, 3) = CStr( ThisWorkbook.Sheets("HOME").Range("K11").value ) And _
( ws.Cells(r, 12).value = 1 Or ws.Cells(r, 12).value = 2 ) _
Then
If ws.Cells(r, 12).value = 1 Then 'show the data with the value of 1
'FIRST BOX
txtBox_LRN.Text = ws.Cells(r, 3).value
txtBox_name.Text = ws.Cells(r, 4).value
txtBox_grd.Text = ws.Cells(r, 5).value
ElseIf ws.Cells(r, 12).value = 2 Then 'show the data with the value of 2
'SECOND BOX
sb_txtBox_LRN.Text = ws.Cells(r, 3).value
sb_txtBox_name.Text = ws.Cells(r, 4).value
sb_txtBox_grd.Text = ws.Cells(r, 5).value
End If
ElseIf _
ws.Cells(r, 3) = CStr(ThisWorkbook.Sheets("HOME").Range("K11").value) And _
( ws.Cells(r, 12).value = 1 Or ws.Cells(r, 12).value = 3 ) _
Then
If ws.Cells(r, 12).value = 1 Then 'show the data with the value of 1
'FIRST BOX
txtBox_LRN.Text = ws.Cells(r, 3).value
txtBox_name.Text = ws.Cells(r, 4).value
txtBox_grd.Text = ws.Cells(r, 5).value
ElseIf ws.Cells(r, 12).value = 2 Then 'show the data with the value of 3
'THIRD BOX
tb_txtBox_LRN.Text = ws.Cells(r, 3).value
tb_txtBox_name.Text = ws.Cells(r, 4).value
tb_txtBox_grd.Text = ws.Cells(r, 5).value
End If
ElseIf _
ws.Cells(r, 3) = CStr( ThisWorkbook.Sheets("HOME").Range("K11").value ) And _
( ws.Cells(r, 12).value = 2 Or ws.Cells(r, 12).value = 3 ) _
Then
If ws.Cells(r, 12).value = 2 Then 'show the data with the value of 2
'SECOND BOX
sb_txtBox_LRN.Text = ws.Cells(r, 3).value
sb_txtBox_name.Text = ws.Cells(r, 4).value
sb_txtBox_grd.Text = ws.Cells(r, 5).value
ElseIf ws.Cells(r, 12).value = 3 Then 'show the data with the value of 3
'THIRD BOX
tb_txtBox_LRN.Text = ws.Cells(r, 3).value
tb_txtBox_name.Text = ws.Cells(r, 4).value
tb_txtBox_grd.Text = ws.Cells(r, 5).value
End If
End If
End If
Next r
The If box = 1 or box = 2 Then and If box = 1 or box = 3 Then are working but I'm having a problem with If box = 2 or box = 3 Then:
If data = 2, it runs in the first IF statement with the this code:
(ws.Cells(r, 12).value = 1 Or ws.Cells(r, 12).value = 2)
or if the data = 3, it runs in this code:
(ws.Cells(r, 12).value = 1 Or ws.Cells(r, 12).value = 3)
But how can I make it run with this?:
(ws.Cells(r, 12).value = 2 Or ws.Cells(r, 12).value = 3) Then
This seems to be what your code is doing but I'm not sure it's correct...
Dim box, kValue
If Application.CountIf(Worksheets(ws_output).Columns(3), LRN) = 2 Then 'data
kValue = CStr( ThisWorkbook.Sheets("HOME").Range("K11").value )
For r = 9 To Lastrow
If ws.Cells(r, 3) = kValue Then
box = ws.Cells(r, 12).value
If box = 1 Then
txtBox_LRN.Text = ws.Cells(r, 3).value
txtBox_name.Text = ws.Cells(r, 4).value
txtBox_grd.Text = ws.Cells(r, 5).value
Elseif box = 2 Then
sb_txtBox_LRN.Text = ws.Cells(r, 3).value
sb_txtBox_name.Text = ws.Cells(r, 4).value
sb_txtBox_grd.Text = ws.Cells(r, 5).value
Elseif box = 3 Then
tb_txtBox_LRN.Text = ws.Cells(r, 3).value
tb_txtBox_name.Text = ws.Cells(r, 4).value
tb_txtBox_grd.Text = ws.Cells(r, 5).value
End if
end if
Next r
end if
EDIT - slightly shorter:
Dim box As Long, kValue, pref As String
If Application.CountIf(Worksheets(ws_output).Columns(3), LRN) = 2 Then 'data
kValue = CStr(ThisWorkbook.Sheets("HOME").Range("K11").Value)
For r = 9 To Lastrow
If ws.Cells(r, 3) = kValue Then
box = ws.Cells(r, 12).Value
If box >= 1 And box <= 3 Then
pref = Array("", "sb_", "tb_")(box - 1) 'get the control name prefix
Me.Controls(pref & "txtBox_LRN").Text = ws.Cells(r, 3).Value 'reference controls by name...
Me.Controls(pref & "txtBox_name").Text = ws.Cells(r, 4).Value
Me.Controls(pref & "txtBox_grd").Text = ws.Cells(r, 5).Value
End If
End If
Next r
End If

macros can't see a worksheet - 'runtime error 9'

I have 2 sheets and 1 macros that pastes values from one to another. The macros is working. I copied it and changed it a bit. But it can't run -
'run time error 9'
which is visibility issue.
All sheets are in same excel file.
original macros code, it works:
Sub original()
For j = 18 To 28
Worksheets("Express_vnzp").Select
srok = Cells(26, j).Value
stav = Cells(31, j).Value
komis = Cells(28, j).Value
stavka_privlech = Cells(29, j).Value
For i = 10 To 12
PD = Cells(i, 17).Value
Worksheets("Ðàñ÷åòû").Select
Cells(3, 2).Value = stav
Cells(4, 2).Value = srok
Cells(5, 2).Value = komis
Cells(7, 2).Value = stavka_privlech
Cells(15, 2).Value = PD
marzha2 = Cells(23, 2).Value
Worksheets("Express_vnzp").Select
Cells(i, j).Value = marzha2
Next
Next
End Sub
I copied and changed i,j - not working.
Sub erj()
For j = 3 To 4
Worksheets("creditcard").Select
srok = Cells(26, j).Value
stav = Cells(31, j).Value
komis = Cells(28, j).Value
stavka_privlech = Cells(29, j).Value
For i = 5 To 6
PD = Cells(i, 17).Value
Worksheets("ras").Select
Cells(3, 2).Value = stav
Cells(4, 2).Value = srok
Cells(5, 2).Value = komis
Cells(7, 2).Value = stavka_privlech
Cells(15, 2).Value = PD
marzha2 = Cells(23, 2).Value
Worksheets("creditcard").Select
Cells(i, j).Value = marzha2
Next
Next
End Sub
gives 'runtime error', its visibility issue.

Finding repeating data from data base

I have slight problem with my code. I have code that extracts data from a DB and puts it into a table. Example:
I want the cells in L column to be highlighted (red for example) if the data is repeating itself for 3 shifts. Or if that can't be done, at least some way I could easily see when data is being repeated.
The idea is that I extract data that is out of a specific range.
SQL Code:
sql = "SELECT ID, (SELECT Number FROM WindingStands WHERE ID = TexMeasurements.WindingStandID) as Place, SpindleNumber, " _
& "(SELECT Number FROM Assortments WHERE ID = TexMeasurements.AssortmentID) as Sifrs, " _
& "(SELECT Name FROM Assortments WHERE ID = TexMeasurements.AssortmentID) as Sort, CreationTime, TexPV, TexSP " _
& " FROM TexMeasurements " _
& " WHERE CreationTime > " & fromdate & " AND CreationTime <= " & ToDate & " " _
& " AND (TexLimit <= -3 OR TexLimit >= 3) ORDER BY Place, SpindleNumber, CreationTime"
I use code to view the data for each day like so:
Sub LastShift()
If (Sheets(1).Cells(1, 1).Value - Sheets(1).Cells(1, 2).Value) > 0.75 Then
Sheets(1).Cells(2, 6).Value = Sheets(1).Cells(1, 2).Value + 0.75
Sheets(1).Cells(2, 3).Value = Sheets(1).Cells(2, 6).Value - 0.5
End If
If (Sheets(1).Cells(1, 1).Value - Sheets(1).Cells(1, 2).Value) < 0.25 Then
Sheets(1).Cells(2, 6).Value = Sheets(1).Cells(1, 2).Value - 0.25
Sheets(1).Cells(2, 3).Value = Sheets(1).Cells(2, 6).Value - 0.5
End If
If (Sheets(1).Cells(1, 1).Value - Sheets(1).Cells(1, 2).Value) > 0.25 And (Sheets(1).Cells(1, 1).Value - Sheets(1).Cells(1, 2).Value) < 0.75 Then
Sheets(1).Cells(2, 6).Value = Sheets(1).Cells(1, 2).Value + 0.25
Sheets(1).Cells(2, 3).Value = Sheets(1).Cells(2, 6).Value - 0.5
End If
Call ExtractData
Sub ThisShift()
If (Sheets(1).Cells(1, 1).Value - Sheets(1).Cells(1, 2).Value) > 0.25 And (Sheets(1).Cells(1, 1).Value - Sheets(1).Cells(1, 2).Value) < 0.75 Then
Sheets(1).Cells(2, 6).Value = Sheets(1).Cells(1, 2).Value + 0.75
Sheets(1).Cells(2, 3).Value = Sheets(1).Cells(1, 2).Value + 0.25
End If
If (Sheets(1).Cells(1, 1).Value - Sheets(1).Cells(1, 2).Value) < 0.25 Then
Sheets(1).Cells(2, 6).Value = Sheets(1).Cells(1, 2).Value + 0.25
Sheets(1).Cells(2, 3).Value = Sheets(1).Cells(2, 6).Value - 0.5
End If
If (Sheets(1).Cells(1, 1).Value - Sheets(1).Cells(1, 2).Value) > 0.75 Then
Sheets(1).Cells(2, 6).Value = Sheets(1).Cells(1, 2).Value + 1.25
Sheets(1).Cells(2, 3).Value = Sheets(1).Cells(2, 6).Value - 0.5
End If
Call ExtractData
In column K, you can put a formula, like this one:
=IF(AND(MATCH(J4;J3;0);MATCH(J4;J2;0));1;0)
In case cell values J3 and J4 are equal and J2 and J4 are equal, you see 1, otherwise you see #N/A (instead of zero, sorry). You can obviously also work with the the Exact() worksheet function.

Resources