Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Fill Right Variable Number of Columns, then Calculate Using Offset
I am stuck on (what is probably) a simple macro. I am trying to prompt a
user to enter a month into C14 and then enter a number (lets say 12 but it varies) into another InputBox and then spread the months over that range. This is what I have so far: Sub BeginMonth() Dim Mth As String Dim Nbr As Long Mth = InputBox("Please enter a beginning month.") Range("C14").Select ActiveCell = Mth Nbr = InputBox("Please enter total number of months.") Selection.AutoFill Destination:=Range("C14:D14"), Type:=xlFillDefault Range("C14:D14").Select ActiveCell.Offset(0, Nbr).Activate With ActiveSheet .Range("C14").AutoFill Destination:=.Range("C14" & Nbr) End With End Sub It fails on AutoFill line. Also, Id like to do a simple calculation in cell C15, such as =(D10/E27). Then, Id like to fill right as many columns as the user enters (as mentioned above, perhaps 12, but it will vary). Finally, in C16, Id like to do another simple calculation, such as =(C15*K10), and again fill right and then shift right one column (Offset 0,1) and down one row, then right one column and down two rows, and right one column and down 3 rows, etc., until it reaches the final column (12 or whatever the user enters). Im sure it can be done and Im taking a crack at it now. If anyone has any suggestions, please share. If I finish it off before I hear back from anyone Ill post the solution here. Regards, Ryan-- -- RyGuy |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Fill Right Variable Number of Columns, then Calculate Using Offset
Maybe something like:
Option Explicit Sub BeginMonth() Dim Mth As String Dim Nbr As Long Do Mth = InputBox(prompt:="Please enter a beginning month.", _ Default:=Format(Date, "mmmm")) If Mth = "" Then MsgBox "Quitting!" Exit Sub End If If IsDate(Mth & " 1, 2007") Then 'whew, it's ok Exit Do Else MsgBox "Please enter a month" End If Loop ActiveSheet.Range("C14").Value = Mth Nbr = CLng(Application.InputBox _ (prompt:="Please enter total number of months.", Type:=1)) If Nbr < 2 Then MsgBox "Quitting!" Exit Sub End If With ActiveSheet .Range("C14").AutoFill Destination:=.Range("C14").Resize(1, Nbr) End With End Sub Application.inputbox with type:=1 requires that the user enter a number. It makes validating the input easier. ryguy7272 wrote: I am stuck on (what is probably) a simple macro. I am trying to prompt a user to enter a month into C14 and then enter a number (lets say 12 but it varies) into another InputBox and then spread the months over that range. This is what I have so far: Sub BeginMonth() Dim Mth As String Dim Nbr As Long Mth = InputBox("Please enter a beginning month.") Range("C14").Select ActiveCell = Mth Nbr = InputBox("Please enter total number of months.") Selection.AutoFill Destination:=Range("C14:D14"), Type:=xlFillDefault Range("C14:D14").Select ActiveCell.Offset(0, Nbr).Activate With ActiveSheet .Range("C14").AutoFill Destination:=.Range("C14" & Nbr) End With End Sub It fails on AutoFill line. Also, Id like to do a simple calculation in cell C15, such as =(D10/E27). Then, Id like to fill right as many columns as the user enters (as mentioned above, perhaps 12, but it will vary). Finally, in C16, Id like to do another simple calculation, such as =(C15*K10), and again fill right and then shift right one column (Offset 0,1) and down one row, then right one column and down two rows, and right one column and down 3 rows, etc., until it reaches the final column (12 or whatever the user enters). Im sure it can be done and Im taking a crack at it now. If anyone has any suggestions, please share. If I finish it off before I hear back from anyone Ill post the solution here. Regards, Ryan-- -- RyGuy -- Dave Peterson |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Fill Right Variable Number of Columns, then Calculate Using Of
Thanks for the effort Dave. I actually did something slightly different
(thanks to Bernie's code): http://www.microsoft.com/office/comm...=en-us&m=1&p=1 I went with this: Sub BeginMonth() Dim Mth As String Dim Nbr As Long Mth = InputBox("Please enter a beginning month.") Range("C14").Select ActiveCell = Mth Nbr = InputBox("Please enter total number of months.") Range("Z1").Select ActiveCell = Nbr Range("C14").Select Selection.AutoFill Destination:=Range _ (Selection, Selection.Resize(1, Nbr)) Range("C15").Select ActiveCell.FormulaR1C1 = "=R10C4/R27C5" Range("C16").Select ActiveCell.FormulaR1C1 = "=R15C3*R10C11" Range("C17").Select Range("C15:C16").Select Selection.AutoFill Destination:=Range _ (Selection, Selection.Resize(1, Nbr)) Range("C15").Select Selection.End(xlToRight).Select ActiveCell.FormulaR1C1 = "=R10C4/R27C5/2" Range("C16").Select Selection.End(xlToRight).Select ActiveCell.FormulaR1C1 = "=R15C3*R10C11/2" End Sub Now I am struggling with the second (and probably much harder) part....... The values are calculating across just fine, so Range(C15:C16) to Selection.Resize(1, Nbr) fills in nicely. Now I am just trying to figure out how to get Excel to select D16, then filldown one row, then select E16, then filldown two rows, then select F16, then filldown three rows, etc, until I get to the end of the columns, which is defined as Nbr. It must be something like Offset(1,1). There has to be a loop so Excel knows where to stop; basically looping until the column set as Num. Can anyone offer any help with this? Regards, Ryan--- -- RyGuy "Dave Peterson" wrote: Maybe something like: Option Explicit Sub BeginMonth() Dim Mth As String Dim Nbr As Long Do Mth = InputBox(prompt:="Please enter a beginning month.", _ Default:=Format(Date, "mmmm")) If Mth = "" Then MsgBox "Quitting!" Exit Sub End If If IsDate(Mth & " 1, 2007") Then 'whew, it's ok Exit Do Else MsgBox "Please enter a month" End If Loop ActiveSheet.Range("C14").Value = Mth Nbr = CLng(Application.InputBox _ (prompt:="Please enter total number of months.", Type:=1)) If Nbr < 2 Then MsgBox "Quitting!" Exit Sub End If With ActiveSheet .Range("C14").AutoFill Destination:=.Range("C14").Resize(1, Nbr) End With End Sub Application.inputbox with type:=1 requires that the user enter a number. It makes validating the input easier. ryguy7272 wrote: I am stuck on (what is probably) a simple macro. I am trying to prompt a user to enter a month into C14 and then enter a number (letâs say 12 but it varies) into another InputBox and then spread the months over that range. This is what I have so far: Sub BeginMonth() Dim Mth As String Dim Nbr As Long Mth = InputBox("Please enter a beginning month.") Range("C14").Select ActiveCell = Mth Nbr = InputBox("Please enter total number of months.") Selection.AutoFill Destination:=Range("C14:D14"), Type:=xlFillDefault Range("C14:D14").Select ActiveCell.Offset(0, Nbr).Activate With ActiveSheet .Range("C14").AutoFill Destination:=.Range("C14" & Nbr) End With End Sub It fails on AutoFill line. Also, Iâd like to do a simple calculation in cell C15, such as =(D10/E27). Then, Iâd like to fill right as many columns as the user enters (as mentioned above, perhaps 12, but it will vary). Finally, in C16, Iâd like to do another simple calculation, such as =(C15*K10), and again fill right and then shift right one column (Offset 0,1) and down one row, then right one column and down two rows, and right one column and down 3 rows, etc., until it reaches the final column (12 or whatever the user enters). Iâm sure it can be done and Iâm taking a crack at it now. If anyone has any suggestions, please share. If I finish it off before I hear back from anyone Iâll post the solution here. Regards, Ryan-- -- RyGuy -- Dave Peterson |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Fill Right Variable Number of Columns, then Calculate Using Of
Whoa! Pretty Cool stuff Dave! I tried your version and it works great. In
the meantime I tried a few things and got this to work: Sub BeginMonth() Dim Mth As String Dim Nbr As Long Mth = InputBox("Please enter a beginning month.") Range("C14").Select ActiveCell = Mth Nbr = InputBox("Please enter total number of months.") Range("Z1").Select ActiveCell = Nbr Range("C14").Select Selection.AutoFill Destination:=Range _ (Selection, Selection.Resize(1, Nbr)) End Sub Any suggestions as to what to do for the second part? -- RyGuy "Dave Peterson" wrote: Maybe something like: Option Explicit Sub BeginMonth() Dim Mth As String Dim Nbr As Long Do Mth = InputBox(prompt:="Please enter a beginning month.", _ Default:=Format(Date, "mmmm")) If Mth = "" Then MsgBox "Quitting!" Exit Sub End If If IsDate(Mth & " 1, 2007") Then 'whew, it's ok Exit Do Else MsgBox "Please enter a month" End If Loop ActiveSheet.Range("C14").Value = Mth Nbr = CLng(Application.InputBox _ (prompt:="Please enter total number of months.", Type:=1)) If Nbr < 2 Then MsgBox "Quitting!" Exit Sub End If With ActiveSheet .Range("C14").AutoFill Destination:=.Range("C14").Resize(1, Nbr) End With End Sub Application.inputbox with type:=1 requires that the user enter a number. It makes validating the input easier. ryguy7272 wrote: I am stuck on (what is probably) a simple macro. I am trying to prompt a user to enter a month into C14 and then enter a number (letâs say 12 but it varies) into another InputBox and then spread the months over that range. This is what I have so far: Sub BeginMonth() Dim Mth As String Dim Nbr As Long Mth = InputBox("Please enter a beginning month.") Range("C14").Select ActiveCell = Mth Nbr = InputBox("Please enter total number of months.") Selection.AutoFill Destination:=Range("C14:D14"), Type:=xlFillDefault Range("C14:D14").Select ActiveCell.Offset(0, Nbr).Activate With ActiveSheet .Range("C14").AutoFill Destination:=.Range("C14" & Nbr) End With End Sub It fails on AutoFill line. Also, Iâd like to do a simple calculation in cell C15, such as =(D10/E27). Then, Iâd like to fill right as many columns as the user enters (as mentioned above, perhaps 12, but it will vary). Finally, in C16, Iâd like to do another simple calculation, such as =(C15*K10), and again fill right and then shift right one column (Offset 0,1) and down one row, then right one column and down two rows, and right one column and down 3 rows, etc., until it reaches the final column (12 or whatever the user enters). Iâm sure it can be done and Iâm taking a crack at it now. If anyone has any suggestions, please share. If I finish it off before I hear back from anyone Iâll post the solution here. Regards, Ryan-- -- RyGuy -- Dave Peterson |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Fill Right Variable Number of Columns, then Calculate Using Of
RyGuy,
For the second part, use code like Range("C15").Resize(1, Nbr).Formula = "=D10/E27" Range("C16").Resize(1, Nbr).Formula = "=C15*K10" What you really want is unclear, but consider these.. Range("C15").Resize(1, Nbr).Formula = "=D10/$E$27" Range("C16").Resize(1, Nbr).Formula = "=C15*$K$10" As to shifting down and right Dim i As Integer For i = 1 To Nbr - 1 Range("C16").Offset(i, i).Resize(1, Nbr - i).Formula = "=Formula that you want" Next i Not sure what formula you want.... HTH, Bernie MS Excel MVP "ryguy7272" wrote in message ... Whoa! Pretty Cool stuff Dave! I tried your version and it works great. In the meantime I tried a few things and got this to work: Sub BeginMonth() Dim Mth As String Dim Nbr As Long Mth = InputBox("Please enter a beginning month.") Range("C14").Select ActiveCell = Mth Nbr = InputBox("Please enter total number of months.") Range("Z1").Select ActiveCell = Nbr Range("C14").Select Selection.AutoFill Destination:=Range _ (Selection, Selection.Resize(1, Nbr)) End Sub Any suggestions as to what to do for the second part? -- RyGuy "Dave Peterson" wrote: Maybe something like: Option Explicit Sub BeginMonth() Dim Mth As String Dim Nbr As Long Do Mth = InputBox(prompt:="Please enter a beginning month.", _ Default:=Format(Date, "mmmm")) If Mth = "" Then MsgBox "Quitting!" Exit Sub End If If IsDate(Mth & " 1, 2007") Then 'whew, it's ok Exit Do Else MsgBox "Please enter a month" End If Loop ActiveSheet.Range("C14").Value = Mth Nbr = CLng(Application.InputBox _ (prompt:="Please enter total number of months.", Type:=1)) If Nbr < 2 Then MsgBox "Quitting!" Exit Sub End If With ActiveSheet .Range("C14").AutoFill Destination:=.Range("C14").Resize(1, Nbr) End With End Sub Application.inputbox with type:=1 requires that the user enter a number. It makes validating the input easier. ryguy7272 wrote: I am stuck on (what is probably) a simple macro. I am trying to prompt a user to enter a month into C14 and then enter a number (let?Ts say 12 but it varies) into another InputBox and then spread the months over that range. This is what I have so far: Sub BeginMonth() Dim Mth As String Dim Nbr As Long Mth = InputBox("Please enter a beginning month.") Range("C14").Select ActiveCell = Mth Nbr = InputBox("Please enter total number of months.") Selection.AutoFill Destination:=Range("C14:D14"), Type:=xlFillDefault Range("C14:D14").Select ActiveCell.Offset(0, Nbr).Activate With ActiveSheet .Range("C14").AutoFill Destination:=.Range("C14" & Nbr) End With End Sub It fails on AutoFill line. Also, I?Td like to do a simple calculation in cell C15, such as =(D10/E27). Then, I?Td like to fill right as many columns as the user enters (as mentioned above, perhaps 12, but it will vary). Finally, in C16, I?Td like to do another simple calculation, such as =(C15*K10), and again fill right and then shift right one column (Offset 0,1) and down one row, then right one column and down two rows, and right one column and down 3 rows, etc., until it reaches the final column (12 or whatever the user enters). I?Tm sure it can be done and I?Tm taking a crack at it now. If anyone has any suggestions, please share. If I finish it off before I hear back from anyone I?Tll post the solution here. Regards, Ryan-- -- RyGuy -- Dave Peterson |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Fill Right Variable Number of Columns, then Calculate Using Of
Sorry to be unclear, but you figured it out nonetheless!!
I went with this tidbit of code (which is awesome): Dim i As Integer For i = 1 To Nbr - 1 Range("C16").Offset(i, i).Resize(1, Nbr - i).Formula = "=Formula that you want" Next i The formula was "=R15C3*R10C11" Thanks for the code from 7/19/2007 and thanks for the code today!!! Regards, Ryan--- -- RyGuy "Bernie Deitrick" wrote: RyGuy, For the second part, use code like Range("C15").Resize(1, Nbr).Formula = "=D10/E27" Range("C16").Resize(1, Nbr).Formula = "=C15*K10" What you really want is unclear, but consider these.. Range("C15").Resize(1, Nbr).Formula = "=D10/$E$27" Range("C16").Resize(1, Nbr).Formula = "=C15*$K$10" As to shifting down and right Dim i As Integer For i = 1 To Nbr - 1 Range("C16").Offset(i, i).Resize(1, Nbr - i).Formula = "=Formula that you want" Next i Not sure what formula you want.... HTH, Bernie MS Excel MVP "ryguy7272" wrote in message ... Whoa! Pretty Cool stuff Dave! I tried your version and it works great. In the meantime I tried a few things and got this to work: Sub BeginMonth() Dim Mth As String Dim Nbr As Long Mth = InputBox("Please enter a beginning month.") Range("C14").Select ActiveCell = Mth Nbr = InputBox("Please enter total number of months.") Range("Z1").Select ActiveCell = Nbr Range("C14").Select Selection.AutoFill Destination:=Range _ (Selection, Selection.Resize(1, Nbr)) End Sub Any suggestions as to what to do for the second part? -- RyGuy "Dave Peterson" wrote: Maybe something like: Option Explicit Sub BeginMonth() Dim Mth As String Dim Nbr As Long Do Mth = InputBox(prompt:="Please enter a beginning month.", _ Default:=Format(Date, "mmmm")) If Mth = "" Then MsgBox "Quitting!" Exit Sub End If If IsDate(Mth & " 1, 2007") Then 'whew, it's ok Exit Do Else MsgBox "Please enter a month" End If Loop ActiveSheet.Range("C14").Value = Mth Nbr = CLng(Application.InputBox _ (prompt:="Please enter total number of months.", Type:=1)) If Nbr < 2 Then MsgBox "Quitting!" Exit Sub End If With ActiveSheet .Range("C14").AutoFill Destination:=.Range("C14").Resize(1, Nbr) End With End Sub Application.inputbox with type:=1 requires that the user enter a number. It makes validating the input easier. ryguy7272 wrote: I am stuck on (what is probably) a simple macro. I am trying to prompt a user to enter a month into C14 and then enter a number (letâ?Ts say 12 but it varies) into another InputBox and then spread the months over that range. This is what I have so far: Sub BeginMonth() Dim Mth As String Dim Nbr As Long Mth = InputBox("Please enter a beginning month.") Range("C14").Select ActiveCell = Mth Nbr = InputBox("Please enter total number of months.") Selection.AutoFill Destination:=Range("C14:D14"), Type:=xlFillDefault Range("C14:D14").Select ActiveCell.Offset(0, Nbr).Activate With ActiveSheet .Range("C14").AutoFill Destination:=.Range("C14" & Nbr) End With End Sub It fails on AutoFill line. Also, Iâ?Td like to do a simple calculation in cell C15, such as =(D10/E27). Then, Iâ?Td like to fill right as many columns as the user enters (as mentioned above, perhaps 12, but it will vary). Finally, in C16, Iâ?Td like to do another simple calculation, such as =(C15*K10), and again fill right and then shift right one column (Offset 0,1) and down one row, then right one column and down two rows, and right one column and down 3 rows, etc., until it reaches the final column (12 or whatever the user enters). Iâ?Tm sure it can be done and Iâ?Tm taking a crack at it now. If anyone has any suggestions, please share. If I finish it off before I hear back from anyone Iâ?Tll post the solution here. Regards, Ryan-- -- RyGuy -- Dave Peterson |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Fill Right Variable Number of Columns, then Calculate Using Of
Sorry to be unclear, but you figured it out nonetheless!!
I must be lucky today - I've got to go play Lotto while it lasts... Glad to be of help. Bernie MS Excel MVP "ryguy7272" wrote in message ... I went with this tidbit of code (which is awesome): Dim i As Integer For i = 1 To Nbr - 1 Range("C16").Offset(i, i).Resize(1, Nbr - i).Formula = "=Formula that you want" Next i The formula was "=R15C3*R10C11" Thanks for the code from 7/19/2007 and thanks for the code today!!! Regards, Ryan--- -- RyGuy "Bernie Deitrick" wrote: RyGuy, For the second part, use code like Range("C15").Resize(1, Nbr).Formula = "=D10/E27" Range("C16").Resize(1, Nbr).Formula = "=C15*K10" What you really want is unclear, but consider these.. Range("C15").Resize(1, Nbr).Formula = "=D10/$E$27" Range("C16").Resize(1, Nbr).Formula = "=C15*$K$10" As to shifting down and right Dim i As Integer For i = 1 To Nbr - 1 Range("C16").Offset(i, i).Resize(1, Nbr - i).Formula = "=Formula that you want" Next i Not sure what formula you want.... HTH, Bernie MS Excel MVP "ryguy7272" wrote in message ... Whoa! Pretty Cool stuff Dave! I tried your version and it works great. In the meantime I tried a few things and got this to work: Sub BeginMonth() Dim Mth As String Dim Nbr As Long Mth = InputBox("Please enter a beginning month.") Range("C14").Select ActiveCell = Mth Nbr = InputBox("Please enter total number of months.") Range("Z1").Select ActiveCell = Nbr Range("C14").Select Selection.AutoFill Destination:=Range _ (Selection, Selection.Resize(1, Nbr)) End Sub Any suggestions as to what to do for the second part? -- RyGuy "Dave Peterson" wrote: Maybe something like: Option Explicit Sub BeginMonth() Dim Mth As String Dim Nbr As Long Do Mth = InputBox(prompt:="Please enter a beginning month.", _ Default:=Format(Date, "mmmm")) If Mth = "" Then MsgBox "Quitting!" Exit Sub End If If IsDate(Mth & " 1, 2007") Then 'whew, it's ok Exit Do Else MsgBox "Please enter a month" End If Loop ActiveSheet.Range("C14").Value = Mth Nbr = CLng(Application.InputBox _ (prompt:="Please enter total number of months.", Type:=1)) If Nbr < 2 Then MsgBox "Quitting!" Exit Sub End If With ActiveSheet .Range("C14").AutoFill Destination:=.Range("C14").Resize(1, Nbr) End With End Sub Application.inputbox with type:=1 requires that the user enter a number. It makes validating the input easier. ryguy7272 wrote: I am stuck on (what is probably) a simple macro. I am trying to prompt a user to enter a month into C14 and then enter a number (let?Ts say 12 but it varies) into another InputBox and then spread the months over that range. This is what I have so far: Sub BeginMonth() Dim Mth As String Dim Nbr As Long Mth = InputBox("Please enter a beginning month.") Range("C14").Select ActiveCell = Mth Nbr = InputBox("Please enter total number of months.") Selection.AutoFill Destination:=Range("C14:D14"), Type:=xlFillDefault Range("C14:D14").Select ActiveCell.Offset(0, Nbr).Activate With ActiveSheet .Range("C14").AutoFill Destination:=.Range("C14" & Nbr) End With End Sub It fails on AutoFill line. Also, I?Td like to do a simple calculation in cell C15, such as =(D10/E27). Then, I?Td like to fill right as many columns as the user enters (as mentioned above, perhaps 12, but it will vary). Finally, in C16, I?Td like to do another simple calculation, such as =(C15*K10), and again fill right and then shift right one column (Offset 0,1) and down one row, then right one column and down two rows, and right one column and down 3 rows, etc., until it reaches the final column (12 or whatever the user enters). I?Tm sure it can be done and I?Tm taking a crack at it now. If anyone has any suggestions, please share. If I finish it off before I hear back from anyone I?Tll post the solution here. Regards, Ryan-- -- RyGuy -- Dave Peterson |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Calculate number of weeks a variable spans in | Excel Worksheet Functions | |||
sum number of columns based on variable value | Excel Discussion (Misc queries) | |||
Fill variable number of cells with a fixed text | Excel Programming | |||
HIDING A VARIABLE NUMBER OF COLUMNS | Excel Programming | |||
select offset (variable ,1) to offset(variable ,variable) | Excel Programming |