I was wondering if there is a faster way to insert a list of values into a table object. The method i'm using goes like this:
1) Clear the old data from table
2) find add in new date values to the table and letting the table formulas auto-populate.
The problem right now is that this process is incredibly slow as i have about 7 tables and 4k+ dates to populate.
Ordinarily if it's just pasting the values, excel works incredibly fast. But when it comes to table objects, it gets a lot slower for some reason.
I think there's a way to do this a lot faster by manipulating the table object but i haven't had any luck so far.
*table1
1 2 3 4 5 6
A Date 1D 2D 3D 4D 5D
B 1/1/2016 Formula Formula Formula Formula Formula
C 2/1/2016 Formula Formula Formula Formula Formula
D 3/1/2016 Formula Formula Formula Formula Formula
E 4/1/2016 Formula Formula Formula Formula Formula
F 5/1/2016 Formula Formula Formula Formula Formula
G 6/1/2016 Formula Formula Formula Formula Formula
H 7/1/2016 Formula Formula Formula Formula Formula
Dim ws(), datelist() As Variant
For i = 1 To UBound(ws)
For j = 1 To UBound(datelist)
Sheets(CStr(ws(i))).Cells(j + 1, 1) = datelist(j)
Next j
Next i
*ws() refers to array where i store worksheet names
*datelist() refers to the dates i want to paste into the cells.
not sure if my description of the problem is clear enough.
Doing a quick search brought up this answer.
To apply it to your case, instead of: -
For i = 1 To UBound(ws)
For j = 1 To UBound(datelist)
Sheets(CStr(ws(i))).Cells(j + 1, 1) = datelist(j)
Next j
Next i
Use: -
For i = 1 To UBound(ws)
Sheets(CStr(ws(i))).Range("A" & j + 1 & ":A" & j + (1 + UBound(datelist))) = WorksheetFunction.Transpose(datelist)
Next i
Related
What I'm trying to achieve is there are 2 whole numbers in column A & B on the same row. I want to fill the row from Column C to show the whole numbers increments of one between the two numbers.
i.e.
A B C D E F G H I J K L
1 10 1 2 3 4 5 6 7 8 9 10
any help would be appreciated.
Assuming this is Excel and you can open the VBE Editor to use VBA
Here's a macro you can run or call via a button
See the comments in the code to understand what it's doing with the Dataseries fill function
Sub FillData()
Dim intStopAt As Integer
' Set to cell indicated low end of range
Cells(1, 1).Select
' Fill in "Start At" Number
ActiveCell.Offset(0, 2).Value = ActiveCell.Value
' Retrieve and use stop number to fill in series
intStopAt = ActiveCell.Offset(0, 1).Value
ActiveCell.Offset(0, 2).DataSeries Rowcol:=xlRows, Type:=xlLinear, Date:=xlDay, Step:=1, Stop:=intStopAt
End Sub
The below code assumes you have no header and that your value in A1 is always 1, and your value in B1 is the number you want to count to.
This can be modified to be more dynamic, but taking your question as is, this should work for you.
1) Check number to count to (CountTo)
2) Run loop for 1 to CountTo and auto-populate your column headers
To run: Open VBE and paste this code on the sheet where you wish to run it.
Sub Counter()
Dim CountTo As Integer
CountTo = Range("B1").Value
For i = 1 To CountTo
Cells(1, i + 2) = i
Next i
End Sub
This can be done without VBA, perhaps not as neat initially as #dbmitch's answer because the formula has to go across to the maximum possible number.
A1 is start number, > 0
B1 is end number (> A1)
In Cell C1 enter =A1
In Cell D1 enter =IF(AND(C1<$B1,C1>=$A1),C1+1,"") and then
drag/fill right as far as you need to.
I have formulated the code so that you can now select the filled rows (A through to wherever) and fill down.
A simple explanation:
C1 sets the start of the list
The AND formula in D1 onwards checks that the immediate left cell (for D1 this is C1, for E1 this is D1 etc.) is less than the end number and greater than the start number.
If the conditions are true, use the immediate left cell value + 1 as the result.
If the conditions are false, insert a blank.
Further checking can be done, I have assumed in the above solution that the numbers are positive and increasing.
You can use helper columns to indicate if you should increase or decrease (i.e. +1 or -1 as required.
Using a blank as the other answer falls down if the numbers go from -ve to +ve. In this case, you could use another symbol (e.g. x) and check for that in the AND function as well.
you could use this:
Sub main()
Dim cell As Range
With Range("A1", Cells(Rows.Count, 1).End(xlUp)).SpecialCells(xlCellTypeConstants, xlNumbers) ' reference column A cells from row 1 down to last not empty one with a "constant" (i.e. not a formula result) numeric content
For Each cell In .Cells 'loop through referenced range
cell.Offset(, 2).Resize(, cell.Offset(, 1).Value - cell.Value + 1).FormulaR1C1 = "=COLUMN()-COLUMN(C3)+RC1" 'write proper formula in current cell adjacent cells
Next
.CurrentRegion.Value = .CurrentRegion.Value ' get rid of formulas and leave values only
End With
End Sub
I have what I thought was a very basic VBA challenge, and have spent hours searching for an answer. Thanks if someone can point me to the right place if already addressed.
I have a formula that is B1 + C1 = D1, and have two 1x5 matrix of data inputs, one for cell B1 and one for cell C1, say [1,2,3,4,5] and [A,B,C,D,E], respectively, in cells (B2:B7) and (C2:C7). I would like to loop through the inputs, such that I get five unique answers [1+A, 2+B, 3+C, 4+D, 5+E], and output those answers in an adjacent 1x5 matrix, say in cells (D2:D7).
Recording a macro does not work here, as it records a copy/paste action that is inflexible for future use (for expanded matrices, other sheet locations, more complex formulas, etc).
Any help much appreciated.
Henry
UPDATE: I believe I need to be using "Do While" or some similar loop code, and additional "For" and "Next" coding.
UPDATE: Here is a step-by-step picture of what I am trying to do with the code:
step-by-step process results image
Here's the solution code:
Sub IterationMacro()
'Declare Variables
Dim h, i, j, k As Integer
Dim mySheet As Worksheet
Dim myAnswer As String
'Set Worksheet
Set mySheet = ActiveSheet
'Set # of Iterations
h = Range("B2").Value
'Clear Previous Contents
Range("C4:D4").ClearContents
Range("e5:e11").ClearContents
'Run Through Loops
For i = 5 To h + 4
For j = 3 To 4
mySheet.Cells(4, j).Value = mySheet.Cells(i, j).Value
Next
'Calculate Workbook
Calculate
mySheet.Cells(i, 5).Value = mySheet.Cells(4, 5).Value
Next
End Sub
If you could draw a table or something to use as an example, it might help.
Assuming I'm undersatnding you, you want to use a formula in D1, and fill down to D7, resulting in showing B+C=D in each row:
Range("D1").Formula="=B1+C1"
Range("D1:D7").Filldown
Edit:
Having been given the example image, it looks like you want math to happen in Row 2 (headers in Row 1). In Row 2 you want to pull up values from Row "i" and add them in Row 2, then paste the answer of that sum in Row "i".
Dim i as Integer 'i is the variable for the loop
For i = 3 to 9 'based on the picture, 3 to 9 are the 1 through 7 values
Cells(2,1).Value=Cells(i,1).Value 'pulls up Column A value from the loop to Row 2
Cells(2,2).Value=Cells(i,2).Value 'pulls up Column B value from the loop to Row 2
Cells(2,3).Formula="=A2+B2" 'Sums A2 and B2 into C2
Cells(2,3).Copy Cells(i,3) 'Copies the summed value to Row "i" in Column C
Next i 'Moves to next "i" in the loop
Let me know if that is more to your point.
Edit:
With dynamic ranges, you still know your starting point. You would look at something similar to:
Dim i as Integer
Dim LR as Long
Dim LC as Long
LR=Cells(Rows.Count,"A").End(xlUp).Row
LC=Cells(1,Columns.Count).End(xlToLeft).Column
For i = 3 to LR 'Still starting at 3, because of the example
Cells(2,1).Value=Cells(i,1).Value
Cells(2,2).Value=Cells(i,2).Value
Cells(2,LC+1).Formula="=A2+B2" 'Note the LC+1 goes one row BEYOND the last column
Cells(2,3).Copy Cells(i,LC+1)
Next i
In the last example, you can see syntax for dynamic ranges. Note that LR and LC are defined outside of the loop and do not change for the duration of the subroutine.
I am simply trying to get several values from a range of cells. It is not returning any value. What am I doing wrong?
I have various numbers in cells B2 to B15.
Dim num As String
For n = 5 To 16
num = Worksheets("Info").Cells(2, n).Value
Debug.Print num
Worksheets("Info").Cells(4, n).Value = num
Next n
Thanks.
As per the Microsoft documentation, the parameters for cell indexes are row then column, not column then row. The example on that linked page illustrates this by using 5, 3 for cell C5:
This example sets the font size for cell C5 on Sheet1 to 14 points:
Worksheets("Sheet1").Cells(5, 3).Font.Size = 14
Hence your arguments to Cells should be n, 2 and n, 4 respectively.
I am relatively new to VBA.
I have a 10 digit number (1234567890) in A1 of Sheet 1 and want to copy this data in A1 in sheet 2 with a concatenated value (-qwerty) and again in A2 in sheet 2 with another concatenated value (-abcdef). So the final values in A1 of Sheet 2 will be 1234567890-qwerty and in A2 of Sheet 2 will be 1234567890-abcdef
Similarly, I want to fill n rows of A column in sheet 1 to 2n rows of A column in Sheet 2.
Looking out for VBA help in excel for the same.
Thank you in advance.
I think no need a VBA
just write in excel's Sheet2 column A1, a formula
= Sheet1.A1 & "-qwerty"
and just write in excel's Sheet2 column A2, a formula
= Sheet1.A1 & "-abcdef"
or something like that
Direct value transfer with a string concatenation would seem to be the easiest.
sheet2.cells(1,1) = sheet1.cells(1, 1).value2 & "-qwerty"
sheet2.cells(2,1) = sheet1.cells(1, 1).value2 & "-abcdef"
Bulk transfer can be accomplished with a simple formula and more string concatenation.
with sheet2.cells(1,1).resize(99,1)
.formula = "='Sheet1'!A1&"-qwerty"
.value = .value
end with
After the formula has been filled, the value in the cell is reverted to the value from the formula (thereby removing the formula).
I am writing a macro in excel and I have a 10 by 10 grid that I need to input a vlookup equation into. So for the first column of data I wrote a for loop
For i = 2 to 11
Cells (i, 30) = "=vlookup (E2" & i & ", B:C, 2, False)"
Next i
And it works the way I want it. Now I would like to write an outer for loop that will allow me to go across the columns.
I started with
For j = 30 to 39
For i = 2 to 11
Cells (i, 30) = "=vlookup (E" & i & ", B:C, 2, False)"
Next i
Next j
But I need E to change to F and I can't figure out how to do that.
I also tried using the Range (Cells(__,__),Cells(__,__)).FormulaR1C1 =...
But I couldn't get that to work either, since it is moving the table as well as fixing E2 across the columns.
If anyone can help I would greatly appreciate it. If not I will just write 10 for loops one for each column.
I think all you need to do is create the first formula and add "$" to appropriately anchor the reference rows/columns at which point you can simply copy the formula across the desired rows/columns.
here is a quick synopsis of anchoring: http://excelonthegrid.wordpress.com/2012/09/26/anchor-with-dollars/
i can give you the final answers if you post the desired formula for
a) column 1 row 1
b) column 1 row 2
c) column 2 row 1
d) column 2 row 2