I've got dates in Row 2 and have the following code to insert a column based on whether the date in B1 is less than the date in B2, C2, etc....
Sub Test3()
If DateValue(Cells(1, 2).Value) < DateValue(Cells(2, 2).Value) Then
Range("B2").EntireColumn.Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Cells(2, Selection.Column).Select
ActiveCell.Value = Range("B1")
ElseIf DateValue(Cells(1, 2).Value) < DateValue(Cells(2, 3).Value) Then
Range("C2").EntireColumn.Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Cells(2, Selection.Column).Select
ActiveCell.Value = Range("B1")
End If
End Sub
The above code works and adds a column at the right place and puts a date in row 2 of the column.
Obviously it would be much easier for me to loop this but I am having trouble getting the loop to work. Here is what I have so far:
Sub DateLoopTest()
Dim i As Integer
i = 1
Do Until DateValue(Cells(1, 2).Value) < DateValue(Cells(2, i + 1).Value)
Cells(2, i + 1).EntireColumn.Select
i = i + 1
Loop
ActiveCell.EntireColumn.Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Cells(2, Selection.Column).Select
ActiveCell.Value = Range("B1")
End Sub
I am getting Run-Time error '13': Type mismatch
No matter how much I mess with it I can't get it to loop like I want in my 1st example. Any suggestions
you can refer to this code:
Sub DateLoopTest()
Dim i As Integer
i = 0
'Loop from [B2] offset 0 to 1,2... -> then stop at [b2].offset(0,i)
Do Until (DateValue([b1]) < DateValue(IIf(IsDate([b2].Offset(0, i)), [b2].Offset(0, i), [b1])))
i = i + 1
Loop
[b2].Offset(0, i).EntireColumn.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
[b2].Offset(0, i).Value = [b1]
End Sub
maybe i got it wrong because i'm just looking at your code, try this again, hope it helps :)
Sub DateLoopTest1()
Dim i As Integer, isCellhere As Boolean, isExistCell As Boolean, isRecentday As Boolean: i = 0:
isRecentday = True
'get lastCell index for Loop
Dim iLast As Integer: iLast = Cells(2, 15000).End(xlToLeft).Column
Dim iMax As Integer: iMax = 2 'default
Dim Cellmax As Range: Set Cellmax = [b2] 'default
Dim Datedefault As Variant: Datedefault = #1/1/1000#
If iLast = 1 Then Exit Sub
'Loop until CellMax
For i = 0 To iLast - 2
isCellhere = Datedefault < DateValue(IIf(IsDate([b2].Offset(0, i)), [b2].Offset(0, i), Datedefault))
'stop if True
If isCellhere Then Set Cellmax = [b2].Offset(0, i): Datedefault = DateValue([b2].Offset(0, i).Value)
Next i
Cellmax.EntireColumn.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Cellmax.Offset(0, -1).Value = [b1]
End Sub
Related
I am trying to insert a column based on a date in cell B1. I have various columns with dates in row 2; however, my supplied code throws off error 1004 when I run it. I believe this is due to the loop eventually running into a cell in row 2 that is empty, presumably because the date in cell B1 is more recent than all the other dates. How can I make it insert a column to the right of the last column with a date in this case?
Here is what I have so far as supplied by user "The GridLock":
Sub DateLoopTest()
Dim i As Integer
i = 0
'Loop from [B2] offset 0 to 1,2... -> then stop at [b2].offset(0,i)
Do Until (DateValue([b1]) < DateValue(IIf(IsDate([b2].Offset(0, i)), [b2].Offset(0, i), [b1])))
i = i + 1
Loop
[b2].Offset(0, i).EntireColumn.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
[b2].Offset(0, i).Value = [b1]
End Sub
It is best to use variables that mean something and avoid using brackets for the ranges. You can't test the logic if you can't identify the range. Your logic doesn't test if there is a date greater than your starting point or if the date already exists in the range. This is a starting point and you can modify to test other logic, but it won't give you an error.
Sub DateLoopTest()
Dim LC As Long
Dim MaxDate As Date
Dim TargetDate As Date
LC = Cells(2, Columns.Count).End(xlToLeft).Column
Dim HdrRng As Range
Set HdrRng = Range(Cells(2, 2), Cells(2, LC))
MaxDate = WorksheetFunction.Max(HdrRng)
TargetDate = Cells(1, 2)
i = 2
If TargetDate < MaxDate And WorksheetFunction.CountIf(HdrRng, TargetDate) = 0 Then
Do Until TargetDate > Cells(2, i)
i = i + 1
Loop
Cells(2, i).EntireColumn.Insert Shift:=xlToRight, copyorigin:=xlFormatFromLeftOrAbove
Cells(2, i).Offset(0, 1) = TargetDate
ElseIf WorksheetFunction.CountIf(HdrRng, TargetDate) > 0 Then
Z = HdrRng.Find(TargetDate).Column
Cells(2, Z + 1).EntireColumn.Insert Shift:=xlToRight, copyorigin:=xlFormatFromLeftOrAbove
Cells(2, Z + 1) = TargetDate
End If
End Sub
Hello StackOverflowers,
I need som help here.
I have been working on a VBA code but it is taking around 20 to 30 mins to process the data and I need some advise to reduce the amount the processing time.
I have 3 sheets in the document.
1- Sheet 1 is called "ExtractData".
That sheet contains 3 columns:
Column A: contains "Environment: PROD, Pre-Prod & UAT", responsible fetching the data based on the environment stated in the dropdown list.
That column contains also a possibility to parse the html text contained in some cells
Column B: Contain list of Product Code
Column C: contains name of fields / attribute for which we need the data for.
Also, we have a button in that sheet that should run the code to fetch the data and display them in sheet called "Source Data"
2- Sheet 2: Called "DataReview", containing extracted data, then I copy data content from cell A2:MJ500 and paste it in sheet 3 (Source Data) that contains some predefined headers.
So I paste data from A4
3- Sheet 3 called: "Source Data"
That sheet will display all data fetched based on the stated attribute
CASE 1: What I am supposed to do, is to filter the data based on some variable and transpose them in a separate sheet:
Exemple 1: May via a VBA buttong, I select specific attribute, like filter based on "Product Family", when you click run, it will copy the data,
then transpose them in a specific way in a separate sheet named after the Product family name
BUT, I tried, different ways and I am not getting what I wanted.
Below find the code I am using, please go through it and help me make it better.
Function Get_File(Enviromment As String, Pos_row As Integer, Data_date As String) As String
Dim objRequest As Object
Dim blnAsync As Boolean
Dim strResponse As String
Dim Token As String
Dim Url As String
Dim No_product_string As String
Token = "xxxxxxxx"
Url = CreateURL(Enviromment, Pos_row, Data_date)
Set objRequest = CreateObject("MSXML2.XMLHTTP")
blnAsync = True
With objRequest
.Open "GET", Url, blnAsync
.SetRequestHeader "Content-Type", "application/json"
.SetRequestHeader "x-auth-token", "xxxxxxxx"
.Send
'spin wheels whilst waiting for response
While objRequest.ReadyState <> 4
DoEvents
Wend
strResponse = .ResponseText
End With
Debug.Print strResponse
Get_File = strResponse
End Function
Function CreateURL(Enviroment As String, Pos_row As Integer, Data_date As String)
Dim product_code As String
If (StrComp(Enviroment, "UAT", vbTextCompare) = 0) Then
CreateURL = "https://TEST1-uat.Nothing.net:8096/api/products/hierarchies"
ElseIf (StrComp(Enviroment, "PPROD", vbTextCompare) = 0) Then
CreateURL = "https://TEST1-pprod.nothing.net:8096/api/products/hierarchies"
ElseIf (StrComp(Enviroment, "PROD", vbTextCompare) = 0) Then
CreateURL = "https://TEST1.nothing.net:8096/api/products/hierarchies"
Else
CreateURL = "https://TEST1.nothing.net:8096/api/products/hierarchies"
End If
If Pos_row <> -1 Then
product_code = ThisWorkbook.Sheets("DataReview").Cells(Pos_row, 1)
CreateURL = CreateURL & "?query=%7B%22productCode%22%3A%22" & product_code & "%22%7D"
End If
If Not (Trim(Data_date & "") = "") Then
CreateURL = Left(CreateURL, Len(CreateURL) - 3) & "%2C%22date%22%3A%22" & Data_date & "%22%7D"
End If
End Function
Function Get_value(Json_file As String, Field_name As String, Initial_value As String, Current_amount_values As Integer) As String
Dim tempString As String
Dim Value As String
Dim Field_name_temp As String
Field_name_temp = "my_" & Field_name 'Ensure that field name is not subset of other field name
Value = Initial_value
Pos_field = InStr(Json_file, Field_name_temp & """:")
tempString = Mid(Json_file, Pos_field + Len(Field_name_temp) + 4)
'MsgBox (Mid(tempString, 1, 75))
If Not StrComp(Left(tempString, 1), "}") Then
Value = Value & "," & ""
Else
Value = Value & "$" & Replace(Split(tempString, "]")(0), """", "")
End If
If Not InStr(tempString, Field_name_temp & """:") = 0 Then
Value = Get_value(tempString, Field_name, Value, Current_amount_values + 1)
End If
Get_value = Value
End Function
Sub Set_value(Value As String, Pos_col As Integer, Pos_row As Integer, Pos_row_max As Integer)
Dim i As Integer
Dim HTML As String
HTML = ThisWorkbook.Sheets("ExtractData").Range("A8")
If HTML = "Yes" Or HTML = "" Then
Value = ParseHTML(Value)
End If
If Value <> "" Then
If UBound(Split(Value, "$")) = 0 Then
ThisWorkbook.Sheets("DataReview").Cells(Pos_row, Pos_col).Value = Value
Else
If Pos_row < Pos_row_max And ThisWorkbook.Sheets("DataReview").Cells(Pos_row + 1, 1) <> "" Then
ThisWorkbook.Sheets("DataReview").Cells(Pos_row, Pos_col).Value = Split(Value, "$")(0)
For i = 1 To UBound(Split(Value, "$"))
ThisWorkbook.Sheets("DataReview").Cells(Pos_row, Pos_col).Offset(1).EntireRow.Insert
ThisWorkbook.Sheets("DataReview").Cells(Pos_row + 1, Pos_col).Value = Split(Value, "$")(i)
Next i
End If
ThisWorkbook.Sheets("DataReview").Cells(Pos_row, Pos_col).Value = Split(Value, "$")(0)
For i = 1 To UBound(Split(Value, "$"))
ThisWorkbook.Sheets("DataReview").Cells(Pos_row + i, Pos_col).Value = Split(Value, "$")(i)
Next i
End If
End If
End Sub
Public Function ParseHTML(ByVal Value As String) As String
Dim htmlContent As New HTMLDocument
htmlContent.body.innerHTML = Value
ParseHTML = htmlContent.body.innerText
End Function
Sub Main_script()
Dim Pos_col As Integer, Pos_row As Integer, Json_file As String, Field_name As String
Dim Value As String
Dim i As Integer
Dim tempValue As String
Dim Pos_row_max As Integer
Dim Enviromment As String
Dim Data_date As String
Pos_col = 2
Pos_row = 2
Call Prepare_sheet
Data_date = Format(ThisWorkbook.Sheets("ExtractData").Range("A5"), "YYYY-MM-DD")
Enviromment = ThisWorkbook.Sheets("ExtractData").Range("A2")
Do While Not IsEmpty(ThisWorkbook.Sheets("DataReview").Cells(Pos_row, 1).Value)
Json_file = Get_File(Enviromment, Pos_row, Data_date)
Do While Not IsEmpty(ThisWorkbook.Sheets("DataReview").Cells(1, Pos_col).Value)
Field_name = ThisWorkbook.Sheets("DataReview").Cells(1, Pos_col).Value
Value = Mid(Get_value(Json_file, Field_name, "", 0), 2) 'Mid() is used to remove "," from the front of values
Pos_row_max = Application.Max(Pos_row_max, Pos_row + UBound(Split(Value, "$")))
Call Set_value(Value, Pos_col, Pos_row, Pos_row_max)
Pos_col = Pos_col + 1
Loop
Pos_col = 2
Pos_row = Pos_row_max + 1
Loop
ThisWorkbook.Sheets("DataReview").Activate
'Columns.AutoFit
'Rows.AutoFit
Cells.Select
Selection.ColumnWidth = 32
Selection.RowHeight = 15
ThisWorkbook.Sheets("DataReview").Range("A2:HM10000").Select
Selection.Copy
Sheets("Source Data").Select
Sheets("Source Data").Range("A4:HM14000").Select
ActiveSheet.Paste
ThisWorkbook.Sheets("Source Data").Activate
End Sub
Sub Prepare_sheet()
Dim i As Integer
Dim j As Integer
i = 2
j = 2
ThisWorkbook.Sheets("DataReview").Range("A1:HH10000").ClearContents
Do While ThisWorkbook.Sheets("ExtractData").Cells(i, 2).Value <> ""
ThisWorkbook.Sheets("DataReview").Cells(i, 1).Value = ThisWorkbook.Sheets("ExtractData").Cells(i, 2).Value
i = i + 1
Loop
Do While ThisWorkbook.Sheets("ExtractData").Cells(j, 3).Value <> ""
ThisWorkbook.Sheets("DataReview").Cells(1, j).Value = ThisWorkbook.Sheets("ExtractData").Cells(j, 3).Value
j = j + 1
Loop
ThisWorkbook.Sheets("DataReview").Cells(1, 1).Value = "Product_code"
End Sub
Sub Insert_product_codes(Value As String)
For i = 1 To UBound(Split(Value, ","))
ThisWorkbook.Sheets("Data").Cells(i, 1).Value = Split(Value, ",")(i)
Next i
End Sub
Module 1 (Containing most of the code):
Module 2 (To transpose data): Here I transpose Data from "Source Data" sheet into "Report" sheet that contains some predefined values in column A
Sub Transpose_Data()
'
' Transpose_Data Macro
'
'
Sheets("Source Data").Select
Rows("4:500").Select
Selection.Copy
Sheets("QRA Report Main").Select
Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("B6:MJ6").Select
Application.CutCopyMode = False
Selection.Insert Shift:=xlDown
Range("B12:MJ12").Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Range("B17:MJ17").Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Range("B23:MJ23").Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Range("B28:MJ28").Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Range("B36:MJ36").Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Range("B45:MJ45").Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Range("B51:MJ51").Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Range("B54:MJ54").Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Range("B61:MJ61").Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Columns("B:NZ").Select
Range("B3").Activate
Selection.ColumnWidth = 30
With Selection
.HorizontalAlignment = xlRight
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
End With
ActiveWorkbook.Save
End Sub
But again as i said, I do not get exactly what I need plus, the processing time is huge.
Try this, but note you'll need to fill in the section in the middle.
``
Sub Transpose_Data()
'
' Transpose_Data Macro
'
'
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Sheets("Source Data").Rows("4:500").Copy Sheets("QRA Report Main").Range("B4")
With Sheets("QRA Report Main")
.Range("B6:MJ6").Insert Shift:=xlDown
.Range("B12:MJ12").Resize(2).Insert Shift:=xlDown
.Range("B17:MJ17").Resize(2).Insert Shift:=xlDown
.Range("B23:MJ23").Resize(2).Insert Shift:=xlDown
' add rest in here
.Range("B61:MJ61").Resize(2).Insert Shift:=xlDown
With .Columns("B:NZ")
.ColumnWidth = 30
.HorizontalAlignment = xlRight
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
End With
End With
ActiveWorkbook.Save
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
``
In the Excel VBA debugger I am getting a
1004 Application-defined or object-defined error.
The line with the error is marked with a note below. I believe that the .Range(Cells(2, j + 1)) object is causing the error but I believe that my syntax is correct. So, I am unsure why this error is occurring.
Option Explicit
Sub Macro2()
Dim i as Integer, j As Integer, lrow As Long, rng As Range, cell As Range, size as Integer
Sheets("Std. BOMs").Select
size = Application.WorksheetFunction.CountIf(Range("F3:F999"), "Total")
ReDim arrVal(1 To size) As Long
lrow = Cells(Rows.Count, "F").End(xlUp).Row
set rng = Range("F3:F" & lrow)
For j = 0 To 1991
j = j + 1
ActiveSheet.Range(ActiveSheet.Cells(j,23), ActiveSheet.Cells(j,24)).Select
Selection.Copy
Range("E1:F1").Select
ActiveSheet.Paste
ActiveSheeet.Cells(j,25).Select
Selection.Copy
Range("G1").Select
ActiveSheet.Paste
i = 0
For Each cell In rng
If Application.WorksheetFunction.ISNA(cell) Then
Exit For
ElseIF InStr(1, cell.Value, "Total", vbTextCompare) > 0 Then
i = i + 1
arrVal(i) = cell.Offset(0,1).Value
End If
Next cell
Sheets("Matrix").Range(Cells(2, j + 1)).Resize(i, 1).Value = Application.Transpose(arrVal) '---------Line with 1004 Error----------'
Next j
Application.CutCopyMode = False
ActiveWindow.ScrollRow = 1
Range("H7").Select
End Sub
Try changing that line to
Sheets("Matrix").Cells(2, j + 1)).Resize(i, 1).Value = Application.Transpose(arrVal)
Range() requires 2 Cells
I have some data as follows in Excel. They are in 2 columns as a list. The left column has an employee name and the second column is time/pc.
Employee Time/pc
TE 0.616438356
TE 0.581896552
FL 0.474040632
AD 0.473251029
AD 0.491803279
TE 0.616438356
TE 0.652173913
TE 0.575
CG 0.744680851
JU 0.784313725
JU 0.568181818
JU 0.709459459
JU 0.227272727
AD 0.461956522
AD 0.555555556
JU 0.014285714
JU 0.692307692
I need easily convert this to a range where column 1 is used as the headers, I need this remove the duplicates. Then it needs to list all the values found beside each name, under each name.
Can anyone help me?
I'm fairly new to VBA, but am trying to answer question to support the community. I welcome any/all corrections to this code, but I tested it. It works and it does what they asked. It is not however optimised in any way, shape, or form.
I called the initial Sheet "RawData" and the new sheet "NewData"
Option Explicit
Sub Pivot()
Dim i, m, q As Integer
i = 2
q = 3
Do While Sheets("RawData").Cells(i, 1).Value <> ""
If Sheets("NewData").Range("A1:ZZ1").Find(Sheets("RawData").Cells(i, 1), LookAT:=xlWhole) Is Nothing Then
If Sheets("NewData").Range("A1") <> "" Then
If Sheets("NewData").Range("B1") <> "" Then
Worksheets("RawData").Cells(i, 1).Copy
Worksheets("NewData").Activate
Cells(1, q).Activate
ActiveCell.PasteSpecial xlPasteValues
Worksheets("RawData").Cells(i, 2).Copy
Worksheets("NewData").Activate
Cells(2, q).Activate
ActiveCell.PasteSpecial xlPasteValues
q = q + 1
Else
Worksheets("RawData").Cells(i, 1).Copy
Worksheets("NewData").Activate
Range("B1").Activate
m = ActiveCell.Column
ActiveCell.PasteSpecial xlPasteValues
Worksheets("RawData").Cells(i, 2).Copy
Worksheets("NewData").Activate
Cells(2, m).Activate
ActiveCell.PasteSpecial xlPasteValues
End If
Else
Worksheets("RawData").Cells(i, 1).Copy
Worksheets("NewData").Activate
Range("A1").Activate
m = ActiveCell.Column
ActiveCell.PasteSpecial xlPasteValues
Worksheets("RawData").Cells(i, 2).Copy
Worksheets("NewData").Activate
Cells(2, m).Activate
ActiveCell.PasteSpecial xlPasteValues
End If
Else
Worksheets("RawData").Cells(i, 2).Copy
Worksheets("NewData").Activate
Sheets("NewData").Range("A1:ZZ1").Find(Sheets("RawData").Cells(i, 1), LookAT:=xlWhole).Activate
m = ActiveCell.Column
Worksheets("NewData").Cells(1, m).End(xlDown).Offset(1, 0).Activate
ActiveCell.PasteSpecial xlPasteValues
End If
i = i + 1
Loop
End Sub
Check below code, it will help you get desired result.
Code posted by Emily Alden will work as well. This is shorter way.
Sub test()
Dim raw_data, new_data As Worksheet
Dim new_col_num, data_row_num, i As Long
Dim employee, time As String
Dim rng As Range
data_row_num = 1
new_col_num = 1
Set raw_data = ThisWorkbook.Sheets("raw_data")
Set new_data = ThisWorkbook.Sheets("new_data")
Do Until raw_data.Cells(data_row_num, 1).Value = ""
employee = raw_data.Cells(data_row_num, 1).Value
time = raw_data.Cells(data_row_num, 2).Value
new_data.Range("A1").Activate
Set rng = new_data.Range("A1:XFD1").Find(what:=employee, LookIn:=xlValues, LookAt:=xlWhole, searchorder:=xlByColumns)
If rng Is Nothing Then
new_data.Cells(1, new_col_num).Value = employee
new_data.Cells(2, new_col_num).Value = time
new_col_num = new_col_num + 1
data_row_num = data_row_num + 1
Else
i = new_data.Cells(1, rng.Column).End(xlDown).Row + 1
new_data.Cells(i, rng.Column).Value = time
data_row_num = data_row_num + 1
End If
Loop
End Sub
I have the following VBA Macro -
Sub CopyData()
Application.ScreenUpdating = False
Dim CRow As Integer
Dim CColBRange As String
Dim PColBRange As String
Dim Continue As Boolean
'Select Sheet1
With Sheets("KG9New")
.Select
'Initialize variables
Continue = True
CRow = 1
While Continue = True
CRow = CRow + 1
'Test B2:
If CRow = 2 And Cells(CRow, 2).Value = 0 Then
Range("A" & CStr(CRow) & ":C" & CStr(CRow)).Copy
Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
CRow = CRow + 1
End If
CColBRange = "B" & CStr(CRow)
PColBRange = "B" & CStr(CRow - 1)
'Break loop upon finding blank cell.
If Len(Range(CColBRange).Value) = 0 Then
Continue = False
End If
'Copy first instance of each changing Value in MachineRunning.
If Range(CColBRange).Value <> Range(PColBRange).Value Then
Range("A" & CStr(CRow) & ":C" & CStr(CRow)).Copy
Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
Wend
End With
Application.ScreenUpdating = True
End Sub
Basically, This scans through Column B of my table and copies values across to a new sheet when the value changes from a 1 to a 0 or 0 to 1.
My issue is that this assumes that the first value (in B2) will be a 1. I would like it to return Row 2 values if B2=0.
I tried changing the initialized CRow to 1, but this returns row 2 whether it is a 1 or 2 (due to it being different from the header, I guess).
Could somebody help me out please?
Change your CRow to 1, like you thought. You can't test B2 if you are never at that cell. Then you just need to do an IF statement.
Sub CopyData()
Application.ScreenUpdating = False
Dim CRow As Integer
Dim CColBRange As String
Dim PColBRange As String
Dim Continue As Boolean
'Select Sheet1
Sheets("KG9New").Select
'Initialize variables
Continue = True
CRow = 1
While Continue = True
CRow = CRow + 1
'Test B2:
If CRow=2 and Cells(CRow, 2).value = 0 Then
CRow = 3
End if
CColBRange = "B" & CStr(CRow)
PColBRange = "B" & CStr(CRow - 1)
'Break loop upon finding blank cell.
If Len(Range(CColBRange).Value) = 0 Then
Continue = False
End If
'Copy first instance of each changing Value in MachineRunning.
If Range(CColBRange).Value <> Range(PColBRange).Value Then
Range("A" & CStr(CRow) & ":C" & CStr(CRow)).Copy
Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
Wend
Application.ScreenUpdating = True
End Sub
The additional If statement just tests to see if we are on row 2 and if that value is 0. If it is, then change CRow to 3 and it will continue on.
I also removed the superfluous With block. I couldn't see anywhere else in the Macro where that format was being used.