Auto fill macro [closed] - excel

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a task to create a loop that fills in columns with the following data:
Column A should populate with a number from 1 to 10000,
Column B should populate with a today's date and increment with the number,
Column C should populate with number of a day of the week so repeating from 1 to 7 every week,
Column D should populate with a day of the week from Monday to Sunday (text).
I think this could be achieved with a For ...Next loop. More complicated would be with the dates...
Could you please give me a hint how to go about this?
THank you

try this
Sub mm()
Dim i As Long
Dim weekDayNames As Variant
With Range("A1:A10000")
.Formula = "=ROW()"
.Offset(, 1).FormulaR1C1 = "=Today()+RC1-1"
.Offset(, 2).FormulaR1C1 = "=WEEKDAY(RC2,1)"
With .Offset(, 1).Resize(, 2)
.Value = .Value
End With
weekDayNames = Application.Transpose(.Offset(, 2).Value)
For i = 1 To UBound(weekDayNames)
weekDayNames(i) = WeekdayName(weekDayNames(i), False, vbSunday)
Next
.Offset(, 3).Value = Application.Transpose(weekDayNames)
End With
End Sub

Related

Overflow error in vba code in addition [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am new in VBA, please help me to resolve below.
While running below facing OVERFLOW error:
total = 0
employee = InputBox("Enter the employee Name")
For Each sheet In Worksheets
For i = 2 To 13
If sheet.Cells(i, 2).Value = employee Then
total = sheet.Cells(i, 3).Value + total
End If
Next i
Next sheet
You are potentially overflowing the total variable as it's probably defaulting to an INTEGER for storage which can only hold up to two bytes (-32768 to 32767).
Instead declare your variable with a type that can hold more data:
Dim total as Long
total = 0
employee = InputBox("ENter the employee Name")
For Each sheet In Worksheets
For i = 2 To 13
If sheet.Cells(i, 2).Value = employee Then
total = sheet.Cells(i, 3).Value + total
End If
Next i
Next sheet

Excel VBA to multiply cell if another cell contains text [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I do know the formula but not sure how to translate into VBA.
Need a VBA script to do the following.
If cell D contains Debit, then value in F multiply -1
If cell D contains Credit, then value in F multiply 1
Loop until last row.
Use this macro:
Sub subMultiply()
For Each cel In Range("D1:D" & Range("D1").End(xlDown).Row)
If cel.Value = "Debit" Then
cel.Offset(0, 2).Value = Val(cel.Offset(0, 2)) * (-1)
ElseIf cel.Value = "Credit" Then
cel.Offset(0, 2).Value = Val(cel.Offset(0, 2)) * 1
End If
Next
End Sub

VBA Find a Row with two variables then time stamp a column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Could anyone give me advice on how to look up a row in a spreadsheet which matches two criteria, then change the data in specific Cells.
The data will be like this..
Reference Version date1 date2 date3
ABC1 1 11/12/2013
ABC1 2 31/12/2013
ABC2 1 12/12/2013
ABC3 1 12/12/2013
ABC1 3 01/01/2014
In VBA I wish to be able to find the row that matches the reference and the version number, then datestamp column 4 of that 1 unique row.
Any help would be really appreciated.
Many Thanks
Solving for a position for two criteria can be solved by this array formula (in this sample looking up ABC3 and 1 in A2:B6
This formula can be used in VBA to provide the position for the timestamp:
VBA Equivalent
Sub OneWay()
Dim lngRow As Long
lngRow = Evaluate("=MATCH(1,(A2:A6=""ABC3"")*(B2:B6=1),0)")
Cells(lngRow + 1, 4) = Now()
End Sub
or just:
Sub OneWay2()
lngRow = Cells(Evaluate("=MATCH(1,(A2:A6=""ABC3"")*(B2:B6=1),0)")+ 1, 4) = Now()
End Sub
Try this code:
Sub test()
Dim refToFind As String
Dim versToFind As String
refToFind = "ABC1"
versToFind = "1"
'address of first reference
Set Rng = Sheet1.Range("B2")
lastrow = Sheet1.Range("B" & Sheet1.Rows.Count).End(xlUp).Row - Rng.Row
For i = 0 To lastrow
If CStr(Rng.Offset(i, 0).Value) = refToFind _
And CStr(Rng.Offset(i, 1).Value) = versToFind Then
Rng.Offset(i, 4) = Now
End If
Next i
End Sub

creating a macro to separate data [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
HI I am new to using excel and only have a basic knowledge in designing macros. I want to be able to design a macro that can separate different invoice details depending on the company unique id into a separate sheet. only problem there is two or three rows that need to be moved together. How would I go about doing this?
For example:
Here is a sample picture of the data. what i want to do is copy the H and N in rows 1 and 2 deepening on the value in row D
Assuming your testing for something like 'is value > 25'
Sub Macro1()
Dim dat As Variant
Dim rng As Range
Dim i As Long
Dim cntr As Integer
cntr = 1
Set rng = [A1:A5]
dat = rng ' dat is now array (1 to 5, 1 to 1)
For i = LBound(dat, 1) To UBound(dat, 1)
If rng(i, 1).Offset(0, 3).Value > 25 Then
Sheets("Sheet2").Range("A" & cntr).Value = Range("A" & i).Value
cntr = cntr + 1
End If
Next
End Sub

EXCEL VBA Speed up my code [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
As per title, the codes below are located in my Private Sub Workbook_open(). Therefore every time I open my workbook it is slow. How do I get to optimize the code to run fastest? Any help is appreciated.
'Sheets("Summary(FG)") ComboBox1 items
For b = 3 To Sheets("CustomerList").Cells(3, 2).SpecialCells(xlLastCell).row
If Sheets("CustomerList").Cells(b, 2) <> "" Then
Worksheets("Summary(FG)").ComboBox1.AddItem (Sheets("CustomerList").Cells(b, 2))
Else
End If
Next
'Sheets("Summary(RawMat)") ComboBox1 items
For a = 2 To Sheets("RawMatList").Cells(2, 2).SpecialCells(xlLastCell).Column
If Sheets("RawMatList").Cells(2, a) <> "" Then
Worksheets("Summary(RawMat)").ComboBox1.AddItem (Sheets("RawMatList").Cells(2, a))
End If
Next
'sheets("Summary(WIP)") ComboBox1 items
For c = 3 To Sheets("WIPList").Cells(3, 2).SpecialCells(xlLastCell).row
If Sheets("WIPList").Cells(c, 2) <> "" Then
Worksheets("Summary(WIP)").ComboBox1.AddItem (Sheets("WIPList").Cells(c, 2))
End If
Next
For Each Worksheet In Worksheets
Application.Goto Reference:=Range("A1"), Scroll:=True
Next Worksheet
It looks like your loop is iterating through every row or every column on a worksheet. Instead of using the last row or last column try using the last used row or last used column. This way instead of moving through thousands of blank rows you only check rows containing data.
Try:
'Sheets("Summary(FG)") ComboBox1 items
For b = 3 To Sheets("CustomerList").UsedRange.Rows.Count
If Sheets("CustomerList").Cells(b, 2) <> "" Then
Worksheets("Summary(FG)").ComboBox1.AddItem (Sheets("CustomerList").Cells(b, 2))
Else
End If
Next
'Sheets("Summary(RawMat)") ComboBox1 items
For a = 2 To Sheets("RawMatList").UsedRange.Columns.Count
If Sheets("RawMatList").Cells(2, a) <> "" Then
Worksheets("Summary(RawMat)").ComboBox1.AddItem (Sheets("RawMatList").Cells(2, a))
End If
Next
'sheets("Summary(WIP)") ComboBox1 items
For c = 3 To Sheets("WIPList").UsedRange.Rows.Count
If Sheets("WIPList").Cells(c, 2) <> "" Then
Worksheets("Summary(WIP)").ComboBox1.AddItem (Sheets("WIPList").Cells(c, 2))
End If
Next
For Each Worksheet In Worksheets
Application.Goto Reference:=Range("A1"), Scroll:=True
Next Worksheet

Resources