encode textbox values to all worksheets

halshion

New Member
Joined
Jan 9, 2023
Messages
16
Office Version
  1. 2013
Platform
  1. Windows
Hello again everyone,

I want to encode the textbox values to all worksheets so I made a code, sourced from the net, modified slightly for my taste. Does not display an error but it doesnt do anything.
what may seems to be the prblem?

VBA Code:
Set wb = Workbooks(Machine1.Value & ".xlsx")
    wb.Activate

    For Each ws In wb.Sheets
    
        'inserts start and date end values to range M4 and R4
        range("M4").Value = CDate(DateFrom.Value)
        range("R4").Value = CDate(DateTo.Value)
        
        datStartDate = range("M4").Value
        datEndDate = range("R4").Value
    
        lngStartDate = datStartDate
        lngEndDate = datEndDate

        Dim i As Date 'autofills date start to end using loop

            For i = 0 To (lngEndDate - lngStartDate)
                Cells(i + 3, 40).Offset(5 * i, 0) = lngStartDate + i
            Next i
    Next ws
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try:
VBA Code:
Cells(i + 3, 40).Offset(5 * i, 0).Value = lngStartDate + i
 
Upvote 0
Try:
VBA Code:
Cells(i + 3, 40).Offset(5 * i, 0).Value = lngStartDate + i
Tried it still didn't encode. The code works if I declare a specific worksheet to encode, maybe i did something wrong with for each loop
 
Upvote 0
Tried it still didn't encode. The code works if I declare a specific worksheet to encode, maybe i did something wrong with for each loop
Oh yes, btw you never use ws. I don't know if you need it for gettin values or setting the values but you should use it. To set values example:
VBA Code:
ws.range("M4").Value = CDate(DateFrom.Value)
ws.range("R4").Value = CDate(DateTo.Value)

ws.Cells(i + 3, 40).Offset(5 * i, 0).Value = lngStartDate + i
Or getting values:
VBA Code:
Range("M4").Value = CDate(ws.DateFrom.Value)
Range("R4").Value = CDate(ws.DateTo.Value)
Or maybe both, I don't know.. These are only my assumptions. You must adjust it according to your needs.

Why did you remove Beluga? He was cute
 
Upvote 0
Oh yes, btw you never use ws. I don't know if you need it for gettin values or setting the values but you should use it. To set values example:
VBA Code:
ws.range("M4").Value = CDate(DateFrom.Value)
ws.range("R4").Value = CDate(DateTo.Value)

ws.Cells(i + 3, 40).Offset(5 * i, 0).Value = lngStartDate + i
Or getting values:
VBA Code:
Range("M4").Value = CDate(ws.DateFrom.Value)
Range("R4").Value = CDate(ws.DateTo.Value)
Or maybe both, I don't know.. These are only my assumptions. You must adjust it according to your needs.

Why did you remove Beluga? He was cute
yep tried it, though not the last one, since the datefrom and dateto values are from userform textboxes

VBA Code:
For Each ws In wb.Worksheets
    
        'inserts start and date end values to range M4 and R4
        ws.range("M4").Value = CDate(DateFrom.Value)
        ws.range("R4").Value = CDate(DateTo.Value)
        
        datStartDate = ws.range("M4").Value
        datEndDate = ws.range("R4").Value
    
        lngStartDate = datStartDate
        lngEndDate = datEndDate

        Dim i As Date 'autofills date start to end using loop

            For i = 0 To (lngEndDate - lngStartDate)
                ws.Cells(i + 3, 40).Offset(5 * i, 0).Value = lngStartDate + i
            Next i
    Next ws

also tried substituting the range to cells format, also didn't work

VBA Code:
For Each ws In wb.Worksheets
    
        'inserts start and date end values to range M4 and R4
        ws.Cells(4, 13).Value = CDate(DateFrom.Value)
        ws.Cells(4, 18).Value = CDate(DateTo.Value)
        
        datStartDate = ws.Cells(4, 13).Value
        datEndDate = ws.Cells(4, 18).Value
    
        lngStartDate = datStartDate
        lngEndDate = datEndDate

        Dim i As Date 'autofills date start to end using loop

            For i = 0 To (lngEndDate - lngStartDate)
                ws.Cells(i + 3, 40).Offset(5 * i, 0).Value = lngStartDate + i
            Next i
    Next ws
 
Upvote 0
Tested. This worked for me:
VBA Code:
For Each ws In wb.Worksheets
  
        'inserts start and date end values to range M4 and R4
        ws.Range("M4").Value = CDate(DateFrom.Value)
        ws.Range("R4").Value = CDate(DateTo.Value)
      
        datStartDate = ws.Range("M4").Value
        datEndDate = ws.Range("R4").Value
  
        lngStartDate = CLng(datStartDate) 'You should cast to Long
        lngEndDate = CLng(datEndDate) 'You should cast to Long

        Dim i As Date 'autofills date start to end using loop

            For i = 0 To (lngEndDate - lngStartDate)
                ws.Cells(i + 3, 40).Offset(5 * i, 0).Value = CDate(lngStartDate + i) 'Convert back again to date
            Next i
    Next ws
Thanks for bringing Beluga back again 🐱
 
Upvote 0
Solution
Tested. This worked for me:
VBA Code:
For Each ws In wb.Worksheets
 
        'inserts start and date end values to range M4 and R4
        ws.Range("M4").Value = CDate(DateFrom.Value)
        ws.Range("R4").Value = CDate(DateTo.Value)
     
        datStartDate = ws.Range("M4").Value
        datEndDate = ws.Range("R4").Value
 
        lngStartDate = CLng(datStartDate) 'You should cast to Long
        lngEndDate = CLng(datEndDate) 'You should cast to Long

        Dim i As Date 'autofills date start to end using loop

            For i = 0 To (lngEndDate - lngStartDate)
                ws.Cells(i + 3, 40).Offset(5 * i, 0).Value = CDate(lngStartDate + i) 'Convert back again to date
            Next i
    Next ws
Thanks for bringing Beluga back again 🐱
Thanks for the help

also cultured Beluga fan 👌
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,588
Members
449,174
Latest member
chandan4057

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