All cells in autofill range being set to nothing Excel - excel

So I am trying to use the autofill method in vba right now over a range that is set to a variable. I know that the range of cells you are autofilling from must be included in the destination. So, I do just that. However, and much to my surprise, all the cells in the range are being set to nothing.
Here is the code:
Dim table2Range As Range
Dim table2Range2 As Range
Dim table2Range3 As Range
Dim tableholder As Range
Set table2Range2 = Range("Y54").End(xlToRight).Offset(0, 1)
Set table2Range3 = Range("Y77").End(xlToRight).Offset(0, 1)
Set table2Range = Range(table2Range2, table2Range3)
Set tableholder = Range("y54", table2Range3)
tableholder.Select
table2Range.AutoFill Destination:=Selection 'This is setting all my cells to nothing for some reason
Here is the before & after screenshots:Before, After
Any help is hugely appreciated!

Your AutoFill line of code should be:
SourceRange.AutoFill Destination:FillRange
It seems like your "table2Range " is the source, which overlap with your Fill Range. I.e:
.End(xlToRight).Offset(0, 1) will set range from Y54 to the rightmost cell of the same row.
You can edit the source range with hardcode first (i.e. Range("Y54:Z77") and see if that works for you, then work from there
For example (try on a new workbook):
Sub example()
Set SourceRange = Worksheets("Sheet1").Range("A1:B1")
Set fillRange = Worksheets("Sheet1").Range("A1:G1")
SourceRange.AutoFill Destination:=fillRange
End Sub
Enter "1" in cell(A1) and "2" in Cell(B1) and run the code.

Related

Can not SUM selected cells With SUMIF

I would like to be able to Check for criteria "owners" In column A and SUM cell from the same row column E with the cell above and repeat through the spread sheet where owner is found.
I have tried to use Function =SUMIF(A3,"*owner*",E2:E3) This returns the Value above the cell in the row with owner but does not SUM them.
I have also tried this VBA CODE and it does the same thing.
Sub vba_sumif()
Dim gRange As Range
Dim sRange As Range
Set gRange = Range("A3")
Set sRange = Range("E2:E3")
Range("G2") = _
WorksheetFunction.SumIf(gRange,"*Owner*", sRange)
End Sub
Ideally it would return the summed cells in the above cell. using VBA
Thanks again,
Aaron
Well first of all, I would suggest just using the formula unless you have a reason not to:
Formula:: Your first error with the formula is both ranges need to be the same size.
VBA:: Same thing here with range sizes, then you also need to remember to make sure Excel knows what sheet you're referring to. I like to do this with a With My_Worksheet_Namestatement, then include a . before any range from that sheet. Just make sure to change the sheet name to your sheet name
Option Explicit
Sub vba_sumif()
Dim TableSheet As Worksheet
Dim gRange As Range
Dim sRange As Range
Set TableSheet = Worksheets("TableSheet")
With TableSheet
Set gRange = .Range("A2:A13")
Set sRange = .Range("E2:E13")
.Range("G2").Value = _
WorksheetFunction.SumIf(gRange, "*Owner*", sRange)
End With
End Sub

Autofill formula in defined column VBA [duplicate]

The following VBA code (Excel 2007) is failing with Error 1004, "Autofill Method of Range Class Failed.". Can anyone tell me how to fix it?
Dim src As Range, out As Range, wks As Worksheet
Set wks = Me
Set out = wks.Range("B:U")
Set src = wks.Range("A6")
src.AutoFill Destination:=out
(note: I have Googled, etc. for this. It comes up fairly often, but all of the responses that I saw had to do with malformed range addresses, which AFAIK is not my problem.
At someone's suggestion I tried replacing the autofill line with the following:
src.Copy out
This had the effect of throwing my Excel session into an apparent infinite loop consuming 100% CPU and then just hanging forever.
OK, apparently the source has to be part of the destination range for autofill. So my code now looks like this:
Dim src As Range, out As Range, wks As Worksheet
Set wks = Me
Set out = wks.Range("B1")
Set src = wks.Range("A6")
src.Copy out
Set out = wks.Range("B:U")
Set src = wks.Range("B1")
src.AutoFill Destination:=out, Type:=xlFillCopy
Same error on the last line.
From MSDN:
The destination must include the
source range.
B:U does not contain A6 and thus there is an error. I believe that you probably want out to be set to A6:U6.
Specifiying just the column name means that you want to fill every row in that column which is unlikely to be the desired behvaiour
Update
Further to the OP's comment below and update to the original answer, this might do the trick:
Dim src As Range, out As Range, wks As Worksheet
Set wks = Me
Set out = wks.Range("B1")
Set src = wks.Range("A6")
src.Copy out
Set out = wks.Range("B1:U1")
Set src = wks.Range("B1")
src.AutoFill Destination:=out, Type:=xlFillCopy
Set out = wks.Range("B:U")
Set src = wks.Range("B1:U1")
src.AutoFill Destination:=out, Type:=xlFillCopy
AutoFill is constrained to a single direction (i.e. horizontal or vertical) at once. To fill a two-dimensional area from a single cell you first have to auto-fill a line along one edge of that area and then stretch that line across the area
For the specific case of copying the formatting and clearing the contents (by virtue of the source cell being empty), this is better:
Dim src As Range, out As Range, wks As Worksheet
Set wks = Sheet1
Set out = wks.Range("B:U")
Set src = wks.Range("A6")
src.Copy out
To make AutoFill work, you need to make the range of AutoFill more than the source range. If the AutoFill range is same as of Source range then there is nothing to AutoFill in that range and hence you would get an error
1004: AutoFill method of Range class failed.
So make AutoFill range more than the source range and error will gone.
If you want to autofill you just do something like...
Private Sub Autofill()
'Select the cell which has the value you want to autofill
Range("Q2").Select
'Do an autofill down to the amount of values returned by the update
Selection.AutoFill Destination:=Range("Q2:Q10")
End Sub
This would autofill down to the specified range.
Does ths help?
Not sure if this helps anyone, but I needed something similar. Selecting the cells as destination works;
dim rowcount as integer
Sheets("IssueTemplate").Select ' Whatever your sheet is
rowcount = 0
rowcount = Application.CountA(Range("A:A"))'get end range
Cells(4, 3).Select 'select the start cell
'autofill to rowcount
Selection.AutoFill Destination:=Range("C4:C" & rowcount), Type:=xlFillDefault
in my example I had to auto-generate a list of folder names from OA100 to OA###?, and this worked fine.

Copy non-contiguous cells and paste in a row retaining number format Excel VBA

I hope someone can help me with this as it's driving me up the wall!
There are 5 non-contiguous cells in a worksheet that I want to copy to the next empty row on another worksheet whilst retaining the number formatting (which varies). I have this so far but am struggling working out how to retain formatting. Can anyone please help? Thanks I anticipation.
`With wsCalc
For bRun = 1 To 4
bData(bRun) = Application.Choose(bRun, .Range("g2"), .Range("b2"), .Range("R2"), .Range("Q14"))
Next bRun
End With
wSResults.Cells(Rows.Count, "a").End(xlUp).Offset(1).Resize(, 4).Value = bData
`
Here's a possible solution, using your hard-coded cell addresses. You will have to set wsCalc and wsResults to their proper worksheets. Slightly more elegant would be to define a "non-contiguous" range on your wsCalc sheet (select the 1st cell, keep Ctrl pressed and select the next one etc, then type a name in the drop-down box just to the left of the formula bar).
Option Explicit
Sub CopyWithFormat()
Dim wsCalc As Worksheet
Set wsCalc = ActiveSheet 'Or whatever your calc sheet is
Dim rngSource As Range
Set rngSource = wsCalc.[G2,B2,R2,Q14]
Dim wsResults As Worksheet
Set wsResults = ActiveSheet 'Or whatever your result sheet is
Dim clDest As Range
Set clDest = wsResults.Cells(Rows.Count, "a").End(xlUp).Offset(1)
Dim cl As Range
For Each cl In rngSource.Cells
clDest.Value = cl.Value
clDest.NumberFormat = cl.NumberFormat
Set clDest = clDest.Offset(1)
Next cl
End Sub
Instead of using .Value, try .Text. It retains formatting. See below.
Gary's Student is right, text is read only, it should be used for the input not the output.
bData(bRun) = Application.Choose(bRun, .Range("g2").Text, .Range("b2").Text, .Range("R2").Text, .Range("Q14").Text)
I also agree with other answer the entire code could be set up more straight forward.

Is there any way to dynamically select an excel column using VBA?

Task: I need to copy a cell value from E12 to the range E23:E25. I achieved this easily using a simple copy and paste statement in the editor. But the range isn't always fixed. I have managed to capture the start point of this range as: Set rangeStart = Range("E12").End(xlDown).Offset(6, 0). I am unable to use this as a starting point for a range selection statement as follows:
Range("E23:E" & Range("A23").End(xlDown).Row).Select
That is how I'm selecting the range to be filled with data in the next step via a paste statement. How do I edit the first half of that range call to something more dynamic? Using rangeStart instead of E23:E.
I have just started working with Excel VBA, so this might be a very basic question to ask. Any help would be appreciated.
First I'd like to address the point that using Select is in most cases highly discouraged. Since you're only looking to paste a value into other cells, you can simply use a line like the following example:
Range("A1:A10").Value = Range("C2:C11").Value
or
Range("A1:A10").Value = 1
This would fill the A1:A10 range with either the values from C2:C11 or the integer 1, respectively. Note the two ranges in the first line are of the same size.
Now, a dynamic approach to your problem could be something along the following example
'Initialise two range variables
Dim SourceRange As Range, TargetRange As Range
'And integers for target rows
Dim fRow As Long, lRow As Long
With ThisWorkbook 'Assuming the code needs to be performed on the Workbook that hosts the macro
'Determine target rows
fRow = 23 'You have not specified how the first row should be determined
lRow = .Cells(fRow, "E").End(xlDown).Row
'Populate range vars
Set SourceRange = .Range("E12")
Set TargetRange = .Range(.Cells(fRow, "E"),.Cells(lRow, "E"))
'Paste values to target range
TargetRange.Value = SourceRange.Value
End With
Note that, since you haven't specified how this should be determined instead, the code above is hard-coded to have a target range in column E that starts at row 23.
If you have
Set rangeStart = Range("E12").End(xlDown).Offset(6, 0)
You can also have
Set rangeEnd = Range(rangeStart, Range("E" & Range("A23").End(xlDown).Row)
which will form a range from the first cell to the second cell.
But read the link posted by Vitaliy.

how to find a value in a range in worksheet code

I need to find a value that the user inputs in one cell, in a range of values in the same workbook. In the worksheet, the user selects a value and then I need to write a code that finds that value in the range or delivers an error message. I was directed to put this in the worksheet code.
I tried specifying the worksheet of the criteria value but that hasn't seemed to make a difference as I am still getting the same error.
Range("Dcust").Offset(2, 0).End(xlDown).End(xlToRight).ClearContents
Dim shuttleNum As Range
Set shuttleNum = Range("Dsched").Offset(2, 0).End(xlDown).End(xlToRight)
Set driverSheet = ActiveWorkbook.Sheets("Driver")
Dim DSnumView As Integer
DSnumView = driverSheet.Range("DSnumView").Value
'''''Here is where I get the error'''
If shuttleNum.Columns(2).Find(DSnumView, lookat:=xlWhole) Is Nothing Then
MsgBox "You are not scheduled to drive this shuttle", vbCritical, "Error"
Exit Sub
Else
Dim ctable As Range
Set ctable = Range("CTableStart").Offset(1,0).End(xlDown).Range("CTableStart").Offset(0, 3)
End If
This line of code:
Range("Dsched").Offset(2, 0).End(xlDown).End(xlToRight)
returns a single cell.
So basically shuttleNum is set to a single cell and consequently this: shuttleNum.Columns(2) will give you an error because there is only one column in this range.
If I understand correctly what you're trying to do, the folowing should help.
Let's assume you have some data which starts from cell A1 but you don't know where it ends. For example:
To get the range that contains the data, which in this case is A1:C5, you would need to do the following:
Dim sht As Worksheet
Dim rng As Range
Set sht = ThisWorkbook.Worksheets("Worksheet Name")
Set rng = sht.Range("A1")
Set rng = sht.Range(rng, rng.End(xlToRight)) 'A1:C1
Set rng = sht.Range(rng, rng.End(xlDown)) 'A1:C5
Alternatively you can merge the last two steps into one:
Set rng = sht.Range(rng, rng.End(xlToRight).End(xlDown))

Resources