VBA code to Autofill a variable number of Columns - excel

I'm new to using VBA, I understand how to autofill a predefined number of columns/rows, however, I'd like the user to be able to input a number into a cell, and starting with a static column/row, the code autofills however many columns the user wants(in this instance how many pay periods an employee would work.) This is what I have so far.
Option Explicit
Sub AutoFill_Test()
'A1 here being where the total number of payperiods are found
Range("A1").AutoFill Destination:=Range("A2:D2"), Type:=xlFillCopy
End Sub
How would I modify this to achieve the desired result?
many thanks,

Related

Is there a better way to Sumif?

I have 18 different ranges on 1 sheet that have names and totals in them. Each range has a list of names with a quantity next to it and a total at the bottom. I can't just search the complete sheet due to the fact that if the same name appears next to every quantity in one range that name also appears next to the total for that range. I need to look up the names and sum the quantities of just the ranges not the totals. I currently use
SUMIF('NC Sign'!$C$3:$C$12,PTS!$B$20,'NC Sign'!$D$3:$D$12)+SUMIF('NC Sign'!$E$31:$E$57,PTS!$B$20,'NC Sign'!$E$31:$E$57)+SUMIF('NC Sign etc....
This is very cumbersome and makes for some extremely long, slow formulas. Does anyone have a better way to do this. P.S. I need this to be accomplished in formula not VBA.
Maybe you may use this
=sum(filter({'NC Sign'!$D$3:$D$12,'NC Sign'!$E$31:$E$57,etc};{'NC Sign'!$C$3:$C$12,'NC Sign'!$E$31:$E$57,etc}='PTS'!$B$20))
But I don't know about how slow it is.

Automatically expand row heights in Excel when referenced from another sheet

Suppose I am having two sheets where on sheet 1 is this kind of data (different length of information within one row):
It might be lot of text that is "wrapped" to fit in a row (decently)
But, when I try to reference the same text, or try to use some formula for instance INDEX/MATCH to get me the same text, I am failing to get proper ROW Height...so I must adjust manually sheet 2 rows to fit nicely from heights...
Is there a way in Excel to automatically make these row expansions? Without taking a manual action every time because I won't be always sure how long it will be my original text...
You could add something into the Worksheet_Change event
Private Sub Worksheet_Change(ByVal Target As Range)
Target.WrapText = True
Target.EntireRow.AutoFit
End Sub
It might get annoying if it runs after every change for every cell, so you should add more if statements to limit which cells trigger the code.

Is there a way of joining list using some criteria?

I am trying to join different lists that have less than 3 values on it, there is an example
.
I was able to do it using macros, but I wanted something more automated because these lists updated quite often, is there a way of doing something like that just by using formulas? If no, is there a way of doing it using VBA but in a better way than "clicking" buttons?
Answering VBasic2008:
I am new to this intermediary/advanced excel functions. I just realized that my Macro didn't work. It does not matter where the results will be, could just be at the end of the table. I have around 20 columns.
Basically, I tried to make something that checks if the number of values in the column is below 3 and if it is it would take this list and paste on the "Group X", but I am trying to use just functions to do that, I was able to make a list with all the columns using INDEX + MOD, but it took all of the names, from AAA to the last one, and I wanted something that would take just the list with less than 3 values.
Some update:
=INDEX(IF($F$11:$J$11=3;$F$6:$J$9;"");MOD(ROW()-ROW($O$6);4)+1;INT((ROW()-ROW($O$6))/4)+1)
I was able to come up with this formula, but everything appears with some spaces.
Thanks,
Matheus
Doing this with formulas is possible, but quite advanced. Someone here will write a formula for you which you may not be able to maintain when your scenario changes.
If you already have VBA code that does what you need doing, then you can move that code to an event procedure that starts automatically when a new value is added to the columns.
The event you need is the Worksheet_Change event and it lives in the Sheet module (right-click the sheet and click "View Code". That will show the Sheet module.
In your scenario, you want to run the code when a value is changed in columns D to G, so your code must start with something like
Private Sub Worksheet_Change(ByVal Target As Range)
' run only when a value in columns D to G are changed
If Not Intersect(Target, Range("D:G")) Is Nothing Then
' your code goes here
MsgBox "You just changed a value in one of the the watched columns!"
End If
End Sub
Edit after additional requirements
If you have Office 365 with the new Dynamic Array functions, this formula approach will work.
Building on your index formula, I made a small change. You can use the modified index formula in a helper column. Then use the Filter() function on the values returned by the Index formulas.
Index formula in cell J4 and copied down to J23
=INDEX(IF($D$11:$H$11=3,$D$4:$H$7,"#"),MOD(ROW()-ROW($J$4),4)+1,INT((ROW()-ROW($J$4))/4)+1)
Filter formula in K4
=FILTER(J4:J23,(J4:J23<>"#")*(J4:J23<>0))
If I understand you correctly, try the following:
I put the formula to count the number of entries in row 2, to make it easier to add items below.
I assumed you mean three or less; and not less than three.
If you have Windows O365, you can use the following formulas: (and change the 100 to the lowest possible row)
D2: =COUNTA(D$4:D$100)
and fill right for the number of columns
I4: =FILTERXML("<t><s>" & TEXTJOIN("</s><s>",TRUE,FILTER($D$4:$G$100,$D$2:$G$2<=3)) &"</s></t>","//s")

How can I format selected cells (column) to 9 digit number

I am trying to make a macro to change a selected column containing 7 digit numbers to 9 digits (2 leading zeros). I have no knowledge of vba. I am trying to learn but the terminology is so vast. In the meantime, I have been using code that I have found on the internet to suit my needs. For example, the code I use for the selection is as follows:
Sub SelectDn()
SelectDn Macro
Range(ActiveCell, ActiveCell.End(xlDown)).Select
End Sub
So I use this to select the data I want to format.
My colleagues at work do a lot of manual manipulation to their excel spreadsheets to create reports and I want to make the process easier for them. I know it's just a matter of right-clicking, Format cells, Custom, 000000000 but would like to do it with less steps.
Is there a way to doing this? Any help would be appreciated.
I tried this:
Selection.NumberFormat = "000000000"
Selection.TextToColumns
I used the macro recorder and formatted the 7 digit number to 9 digit. The code that it generated was:
Sub Macro14()
Selection.NumberFormat = "000000000"
End Sub
But when I try to run the macro again, I get a Compile error: Expected Function or variable and the word "Selection" in Selection.NumberFormat is highlighted. I would like the number to change from 9983743 to 009983743.
You can reference a column to some cell with EntireColumn. This is where you want to apply your format so.....
Sub Digits()
Selection.EntireColumn.NumberFormat = "000000000"
End Sub
Also, .Selection is usually not the best way to refer to a range. Is there some other, more systematic, way you can refer to your target columns? If so, I would consider revising this to do so. Maybe InputBox or pre-code the columns where you expect this format to apply.

Macro to convert many columns in excel from MS to HH:MM:SS

We have a spreadsheet that's being generated by another system that provides data in Milliseconds, but I need to convert it to something that's easier to digest for the business.
I'm looking for an easy way to get it converted to duration HH:MM:SS - the columns to be converted aren't always the same columns so I'd need to be able to select certain columns and then do the conversion.
Any help is greatly appreciated - I played a bit with the macros in Excel, but it's been years since I programmed and I wasn't sure how to do some of the relative mappings for the equations.
Thanks!
Select a contiguous group of cells containing millisecond values as long integers.
Run the convertSelection sub procedure.
Select another contiguous group of cells.
Go to step 2.
Remember to put this code into a public module code sheet first.
Sub convertSelection()
With Selection
.Value = Application.Evaluate(.Address & "/86400000")
.NumberFormat = "hh:mm:ss.000"
End With
End Sub

Resources