Problem with VBA Machine Code vs Binary...Maybe?

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
I have a bit of code that is returning dates values inaccurately. The search values that are generating the problem all begin with 2 as in February. Dates that begin with 2 are being found as December dates. Its as if the find function is adding a "1" before the two somehow. Not sure what to think here? Is this a machine code vs binary problem perhaps?

Code:
RngEnd2 = Cells.Find(What:=EndDate, After:=ActiveCell, LookIn:=xlFormulas, _        LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Address(1, 1)
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
EndDate is a Range Variable, with a value of 2/10/2010 according to Add Watch

RngEnd2 is pasting the value 12/10/2010 in the worksheet
 
Last edited:
Upvote 0
Do either of the below make a difference?

Code:
RngEnd2 = Cells.Find(What:=CDate(EndDate), After:=Cells(1,1), LookIn:=xlFormulas, _        LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Address(1, 1)

Code:
RngEnd2 = Cells.Find(What:=CLng(EndDate), After:=Cells(1,1), LookIn:=xlFormulas, _        LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Address(1, 1)
 
Upvote 0
CDate is still taking a December date for a February. CLng is returning an error on the second iteration "object variable or with block variable not set"
 
Upvote 0
Although I don't think it will make a difference what does the below produce?

Code:
RngEnd2 = Cells.Find(What:=CDate(EndDate), After:=Cells(1,1), LookIn:=xlValues, _        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False).Address(1, 1)
 
Upvote 0
Can you post the full code please so that I can see the variable being created and the paste.
 
Upvote 0
Ultimately I would like to have a the correct range pasted into a location defined by the line below.

Code:
ActiveWorkbook.Sheets("ActiveSheet").Range(StartDate, EndDate).Resize(, 2).Copy

The whole chunk is below

Code:
Dim aa As IntegerDim StartDate As Range
Dim EndDate As Range
Dim RngStart As String
Dim RngEnd  As String
Dim RngEnd2 As String
Dim sh As Worksheet
Dim chrt As ChartObject
Dim ch As Chart


For aa = 5 To 24


    ActiveWorkbook.Sheets("AllDistanceMeasures").Activate


    Set StartDate = Cells(aa, 9)
    Set EndDate = Cells(aa, 10)


    ActiveWorkbook.Sheets("ActiveSheet").Activate
    
        RngStart = Cells.Find(What:=StartDate, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Address(1, 1)
        
        RngEnd = Cells.Find(What:=EndDate, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Address(1, 1)
    
    Set sh = ActiveWorkbook.Worksheets("LowDistCharts")
    Set chrt = sh.ChartObjects.Add(0, 0, 300, 300)
    Set ch = chrt.Chart
    
    With chrt
        .Height = 300
        .Width = 300
        .Top = 1 + ((aa - 4) * 200)
        .Left = 1
    End With
    
    With ch
        .HasTitle = True
        .ChartTitle.Text = aa & StartDate & " to " & EndDate
        .ChartTitle.Font.Size = 8
        .ChartType = xlLine
        .SetSourceData Source:=Range(RngStart & ":" & RngEnd)
        .SeriesCollection.Add (Worksheets(DataSht).Range(DataRange))
        .HasLegend = False
        .SeriesCollection(1).AxisGroup = 2
    End With
    
    ActiveWorkbook.Sheets("ActiveSheet").Range(StartDate, EndDate).Resize(, 2).Copy
    Sheets("LowDistCharts").Range("S3").End(xlToRight).Offset(0, 1).PasteSpecial xlPasteValues


Next aa
 
Upvote 0
Code:
For zz = 0 To NumObs - 1    
        ActiveWorkbook.Sheets("ActiveSheet").Range(EndDate).Resize(, 2).Offset(zz, 0).Copy
        Sheets("LowDistCharts").Range("S3").End(xlToRight).Offset(0, 1).PasteSpecial xlPasteValues
    
    Next zz

I tried something like this attempting to bypass the RngStart and RngEnd assignments, but am receiving an application or object defined error on the first line inside the loop
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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