I'm trying to fill 1299 cells in a row with the value 0530 using the following code:
Sub FillValues()
Dim X As Integer
For X = 2 To 1300
Worksheets("Table1").Range("B" & X).Value = "'0530"
Next X
End Sub
For some reason it doesn't work and I don't know why. The error is "Index beyond the valid range."
Please try (no loop):
Worksheets("Table1").Range("B2:B1300").Value = "'0530"
Related
I'm having an issue with my For If statement. The scenario is that, I need the empty cells to be filled with a telephone number. I have made a code and it works but at some point it will stop in between. Sometimes all the way to the end of the last row or just skip a few cells and continue again. I'm really not sure about this weird issue, hopefully someone can give me an insight. I have also attached a snapshot of the results.
My code is as shown below:-
Dim pn As Range
Dim h As Integer, phone As Integer
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Set pn = Range("AE2:AE" & lastrow)
h = 1
For phone = 1 To pn.Rows.Count
If pn.Cells(h) = "" Then
pn.Cells(h) = "03-33422828"
Else
h = h + 1
End If
Next
An alternative way to do this is to select all the used cells in that range, then fill all the blank ones with a value.
For example, if we have a workbook with data in column A and we want to fill the blank cells in that column with "---" we could do this:
On Error Resume Next
Range("A1:A" & ActiveSheet.UsedRange.Rows.Count). _
SpecialCells(xlCellTypeBlanks).Value = "---"
On Error GoTo 0
VBA will generate an error if there are no blank cells, so I've "handled" that by just ignoring any errors.
For you it would be more like:
On Error Resume Next
Range("AE1:AE" & ActiveSheet.UsedRange.Rows.Count). _
SpecialCells(xlCellTypeBlanks).Value = "03-33422828"
On Error GoTo 0
Im trying to create a module to pull data from a giant spreadsheet that I have on another sheet by year. Every part of the code works except for the part that matches the year to a user entered year.
Here's how I defined the user input and how I tried to write the if statement.
Dim y As variant
y = InputBox("Input year here")
If Year(RptSht.Cells(i, 2)) = y
At this point I get a type mismatch (I've tried setting y as an integer instead). Also just as a note I can use
Year(RptSht.Cells(i, 2))
to get a value, it just mismatches with y. Any help would be appreciated.
If Year(RptSht.Cells(i,2)) = y
That's doing too many things. Split it up.
First you want to get the cell at (i, 2):
Dim yearCellValue As Variant
yearCellValue = RptSht.Cells(i, 2)
Now, we can't just assume that yearCellValue is a valid date. We have to know it, otherwise if anything is wrong with the assumption, we'll likely run into a type mismatch error. Use the IsDate function to make sure you're looking at a Date value:
If IsDate(yearCellValue) Then
End If
Inside that conditional block, Year(yearCellValue) is safe. Outside of it, it isn't.
If IsDate(yearCellValue) Then
If Year(yearCellValue) = y Then
'...
End If
End If
Problem is, we don't know that y is a valid value either.
Dim y As variant
y = InputBox("Input year here")
If Not IsNumeric(y) Then Exit Sub 'bail out, we can't go any further.
Here is one way to handle the issue:
Sub gotimm()
Dim y As Long, RptSht As Worksheet, i As Long
y = Application.InputBox(Prompt:="Input year here", Type:=1)
Set RptSht = ActiveSheet
i = 1
With RptSht
If IsDate(.Cells(i, 2)) Then
If .Cells(i, 2) = y Then
MsgBox "match"
Else
MsgBox "nomatch"
End If
Else
MsgBox "no date in cell " & .Cells(i, 2).Address
End If
End With
End Sub
I guess the problem is that i is not assigned or (i,2) is not a date. Try this:
Sub TestMe()
Dim y As Variant
Dim i As Long
y = InputBox("Input year here")
i = 5
If IsDate(Worksheets(1).Cells(i, 2)) Then
If Year(Worksheets(1).Cells(i, 2)) = y Then
'Logic here
End If
End If
End Sub
Thus, the i is 5 and the reference cell is B5. The IsDate() checks whether the cell is a date.
I would like to use a Vlookup function 47 times to get the value for every data. I call the table I am filling "Table 1". "Table 1 starts from E3. I would like to use the vlookup to find the value for cell E3 and fill it in F3.
I call the table from which I return value by Vlookup "Table2". "Table 2 is located in sheet "CC Name" and has two columns A and B.
I have tried two FOR Loops. One FOR Loop for the Vlookup function to be repeated 47 times. Second FOR Loop for the Name of the vlookup function "ccName" to use the function to fill the value in "Table 1" for 43 times, but I get error every time I implement the Code.
Sub GLcreation()
For n = 3 To 50
For c = 3 To 50
ccName(c) = WorksheetFunction.Vlookup(Range("E" & n), Worksheets("CC Name").Range("A:B"), 2, 0)
Range("F" & n) = ccName(c)
Next c
Next n
End Sub
If you can Show me how to Code the correct for Loop, I appreciate your help.
As soon as there is a non-match the code will fail. It's safer to test for match before making the lookup. Here is a better solution:
Sub GLcreation()
Dim n As Integer
Dim wf As WorksheetFunction
Set wf = WorksheetFunction
For n = 3 To 50
If wf.CountIf(Worksheets("CC Name").Range("A:A"), Range("E" & n).Value) > 0 Then
Range("F" & n).Value = wf.Index(Worksheets("CC Name").Range("B:B"), wf.Match(Range("E" & n).Value, Worksheets("CC Name").Range("A:A"), 0))
End If
Next n
End Sub
By getting rid of c Loop, the correct Code is:
Range("F" & n) = WorksheetFunction.Vlookup(Range("E" & n), Worksheets("CC Name").Range("A:B"), 2, 0)
I am trying to write a VBA function but getting an error , can someone take a look and suggest where I am going wrong.
What the function is doing is looking at Column 1 and checking if its error then assigning value of No to X. Same with Y on a different column.
If either of them have a value or name then the output is Yes, else its a No
Added picture, I am getting a value error right now. But if column F and G are same column H is a yes, if either one has a name H is yes. Only when both are N/A H is a No.
Function Checkmapping(x As Variant, y As Variant)
If x = "#N/A" Then
x = "No"
End If
If y = "#N/A" Then
y = "No"
End If
If x <> y Then
Checkmapping = "Yes"
Else: Checkmaping = "No"
End If
End Function
Calling via
BigBen: I know the formula is easy, but want a VBA function , just so I can add more complex logic later, if there are 10 parameters the formula will get crazy.
several issues here:
A cell containing an #N/A error is not the same as a cell containing the text "#N/A"
If a cell contains an error value (including #N/A comparing it to a string in VBA will throw a run-time error
There is a typo: Checkmaping = "No" vs Checkmapping = "No"
Refactored code
Function Checkmapping(x As Variant, y As Variant)
If Application.IsNA(x) Then
x = "No"
End If
If Application.IsNA(y) Then
y = "No"
End If
If x <> y Then
Checkmapping = "Yes"
Else
Checkmapping = "No"
End If
End Function
Based on your statement Only when both are N/A H is a No. the above won't give that result. Then try this
Function Checkmapping(x As Variant, y As Variant)
Checkmapping = "Yes"
If Application.IsNA(x) Then
If Application.IsNA(y) Then
Checkmapping = "No"
End If
End If
End Function
Bonus: based on your comment I know the formula is easy, but want a VBA function , just so I can add more complex logic later, if there are 10 parameters the formula will get crazy.
Function Checkmapping(r As Range)
Dim cl As Range
Checkmapping = "No"
For Each cl In r
If Not Application.IsNA(cl) Then
Checkmapping = "Yes"
Exit Function
End If
Next
End Function
Call it like =Checkmapping(F10:G10) or =Checkmapping(F10:Z10) to check more cells
And just for completeness, you can do this with a Formula for a variable number of cells
=IF(SUMPRODUCT(IFNA(F10:G10,-1)+1),"Yes","No")
Enter as an Array Formula (complete with Ctrl-Shift-Enter rather than just Enter) and then copy down
Ok so the thing is, I'm writing this for an Excel Sheet, and the problem i've run into is the loop doesn't check to see if the destination cell is empty or not. So what i'm trying to do is check if the cell is empty if it is then do the existing paste. if it's not then keep looking till it finds the first empty box....can someone help with this?
Dim x, z
Set a = Sheets("Working")
Set b = Sheets("Peer Review")
Set c = Sheets("Waiting to Push")
Set d = Sheets("Completed")
x = 1
z = 2
Do Until IsEmpty(a.Range("I" & z))
If a.Range("I" & z) = "Peer" Then
x = x + 1
b.Rows(x).Value = a.Rows(z).Value
Else
If a.Range("I" & z) = "Waiting" Then
x = x + 1
c.Rows(x).Value = a.Rows(z).Value
End If
End If
z = z + 1
Loop
I'd recommend to rewrite the code as follows:
Option Explicit
Public Sub tmpSO()
Dim z As Long
Dim a As Worksheet, b As Worksheet, c As Worksheet, d As Worksheet
Set a = ThisWorkbook.Worksheets("Working")
Set b = ThisWorkbook.Worksheets("Peer Review")
Set c = ThisWorkbook.Worksheets("Waiting to Push")
Set d = ThisWorkbook.Worksheets("Completed")
z = 2
For z = a.Cells(a.Rows.Count, "I").End(xlUp).Row To 2 Step -1
If a.Cells(z, "I").Value2 <> vbNullString Then
Select Case UCase(a.Cells(z, "I").Value2)
Case "PEER"
b.Rows(b.Cells(b.Rows.Count, "I").End(xlUp).Row + 1).Value2 = a.Rows(z).Value2
a.Rows(z).Delete
Case "WAITING"
c.Rows(c.Cells(c.Rows.Count, "I").End(xlUp).Row + 1).Value2 = a.Rows(z).Value2
a.Rows(z).Delete
Case "COMPLETED"
d.Rows(d.Cells(d.Rows.Count, "I").End(xlUp).Row + 1).Value2 = a.Rows(z).Value2
a.Rows(z).Delete
Case Else
MsgBox "Unknown value " & a.Cells(z, "I").Value2 & " in row " & z & Chr(10) & "Skipping to next row..."
End Select
End If
Next z
End Sub
Changes:
Implement a for ... next instead of a loop is most of the time a better coding practice since a loop can potentially lead to an infinite loop and crash your Excel.
Using select case instead of multiple if clauses. This is not really much faster but simply better to read and understand.
I removed x because this would not always use the last row on each sheet. Instead x is incremented on each sheet and thus can lead to empty (in between) rows on all other sheets. Instead, the above code now checks column I for the last row on that sheet and then copies the row from sheet a over to the next available one.
The above code is now (no longer) case sensitive when checking for peer or Peer or pEEr in column I. I am guessing that this better suits your needs.
If an unknown value (other than peer, waiting, or completed) in column I is encountered then you get a message box telling you about it.
In accordance to your request (in the comments below) the above code now deletes any row which has been successfully copied over to another sheet. Yet, unrecognized values in column I cannot be copied over to any other sheet and (as such) stay on sheet a ("Working").
Note, that the above assumes that "empty" is defined as "there in nothing in the cell's formula. If you prefer you can also set it to "if the cell is showing no value" (instead). The difference is that if a cell contains a formula which results in "" then there is a formula in the cell but the value is currently (due to the formula) nothing.