Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 184
Default Visual to automate formula

I am looking for a way to place a formula in h and I in the corosponding row
if there is data in A.

These are my two formulas
=A3-WEEKDAY(A3,2)+1
=A3-DAY(A3)+1

So if there is data in A3 the first formula will be placed in H3 and the
second in I3. If there is no data in A3 there will be nothing in these cells.

Thank you
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default Visual to automate formula

Sub DepositFormula()
Dim r1 As Range, r2 As Range, r3 As Range, n As Long
Dim i As Long
n = Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To n
Set r1 = Cells(i, 1)
Set r2 = r1.Offset(0, 7)
Set r3 = r1.Offset(0, 8)
If IsEmpty(r1) Then
Else
r2.Formula = "=A" & i & "-WEEKDAY(A" & i & ",2)+1"
r3.Formula = "=A" & i & "-DAY(A" & i & ")+1"
End If
Next
End Sub

--
Gary''s Student - gsnu200909
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 184
Default Visual to automate formula

I took a placed the below under sheet one in the visual basic window and it
didn't seem to work. I tried this on a new book.

Thank you

"Gary''s Student" wrote:

Sub DepositFormula()
Dim r1 As Range, r2 As Range, r3 As Range, n As Long
Dim i As Long
n = Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To n
Set r1 = Cells(i, 1)
Set r2 = r1.Offset(0, 7)
Set r3 = r1.Offset(0, 8)
If IsEmpty(r1) Then
Else
r2.Formula = "=A" & i & "-WEEKDAY(A" & i & ",2)+1"
r3.Formula = "=A" & i & "-DAY(A" & i & ")+1"
End If
Next
End Sub

--
Gary''s Student - gsnu200909

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,203
Default Visual to automate formula

Did you use Tools -- Macro -- Macros to try to run it? It worked for me.

However, if you want a solution that fills out the formulas when something
is entered into/changes in column A, then try this in the worksheet's code
module (open the workbook, select the sheet, right-click on the sheet name
and choose View Code from the list, copy and paste the code below into the
new module.

I didn't change the other very much, so actually it's doing more work than
it probably has to, but it does work.

Private Sub Worksheet_Change(ByVal Target As Range)

Dim r1 As Range, r2 As Range, r3 As Range, n As Long
Dim i As Long
If Target.Column < 1 Or Target.Columns.Count 1 Then
Exit Sub
End If
n = Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To n
Set r1 = Cells(i, 1)
Set r2 = r1.Offset(0, 7)
Set r3 = r1.Offset(0, 8)
If IsEmpty(r1) Then
Else
r2.Formula = "=A" & i & "-WEEKDAY(A" & i & ",2)+1"
r3.Formula = "=A" & i & "-DAY(A" & i & ")+1"
End If
Next

End Sub


"Jeremy" wrote:

I took a placed the below under sheet one in the visual basic window and it
didn't seem to work. I tried this on a new book.

Thank you

"Gary''s Student" wrote:

Sub DepositFormula()
Dim r1 As Range, r2 As Range, r3 As Range, n As Long
Dim i As Long
n = Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To n
Set r1 = Cells(i, 1)
Set r2 = r1.Offset(0, 7)
Set r3 = r1.Offset(0, 8)
If IsEmpty(r1) Then
Else
r2.Formula = "=A" & i & "-WEEKDAY(A" & i & ",2)+1"
r3.Formula = "=A" & i & "-DAY(A" & i & ")+1"
End If
Next
End Sub

--
Gary''s Student - gsnu200909

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 184
Default Visual to automate formula

Thank you very much

"JLatham" wrote:

Did you use Tools -- Macro -- Macros to try to run it? It worked for me.

However, if you want a solution that fills out the formulas when something
is entered into/changes in column A, then try this in the worksheet's code
module (open the workbook, select the sheet, right-click on the sheet name
and choose View Code from the list, copy and paste the code below into the
new module.

I didn't change the other very much, so actually it's doing more work than
it probably has to, but it does work.

Private Sub Worksheet_Change(ByVal Target As Range)

Dim r1 As Range, r2 As Range, r3 As Range, n As Long
Dim i As Long
If Target.Column < 1 Or Target.Columns.Count 1 Then
Exit Sub
End If
n = Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To n
Set r1 = Cells(i, 1)
Set r2 = r1.Offset(0, 7)
Set r3 = r1.Offset(0, 8)
If IsEmpty(r1) Then
Else
r2.Formula = "=A" & i & "-WEEKDAY(A" & i & ",2)+1"
r3.Formula = "=A" & i & "-DAY(A" & i & ")+1"
End If
Next

End Sub


"Jeremy" wrote:

I took a placed the below under sheet one in the visual basic window and it
didn't seem to work. I tried this on a new book.

Thank you

"Gary''s Student" wrote:

Sub DepositFormula()
Dim r1 As Range, r2 As Range, r3 As Range, n As Long
Dim i As Long
n = Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To n
Set r1 = Cells(i, 1)
Set r2 = r1.Offset(0, 7)
Set r3 = r1.Offset(0, 8)
If IsEmpty(r1) Then
Else
r2.Formula = "=A" & i & "-WEEKDAY(A" & i & ",2)+1"
r3.Formula = "=A" & i & "-DAY(A" & i & ")+1"
End If
Next
End Sub

--
Gary''s Student - gsnu200909



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 8,651
Default Visual to automate formula

=IF(A3="","",A3-WEEKDAY(A3,2)+1) in H3
=IF(A3="","",A3-DAY(A3)+1) in I3
--
David Biddulph

"Jeremy" wrote in message
...
I am looking for a way to place a formula in h and I in the corosponding
row
if there is data in A.

These are my two formulas
=A3-WEEKDAY(A3,2)+1
=A3-DAY(A3)+1

So if there is data in A3 the first formula will be placed in H3 and the
second in I3. If there is no data in A3 there will be nothing in these
cells.

Thank you



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Automate Formula script Jeremy Excel Discussion (Misc queries) 3 November 25th 09 06:25 PM
Is Visual Basic the same as Visual Studio 2008? Mike Stewart Excel Worksheet Functions 5 January 11th 09 04:58 PM
Automate a formula in excel Farris Excel Discussion (Misc queries) 6 August 9th 07 11:18 PM
automate formula pboost1 Excel Discussion (Misc queries) 2 March 2nd 06 07:52 PM
changing the visual basic in office 2003 to visual studio net bigdaddy3 Excel Discussion (Misc queries) 1 September 13th 05 10:57 AM


All times are GMT +1. The time now is 04:39 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"