跨Varying细胞应用VBA宏?

时间:2017-02-11 20:06:49

标签: excel vba excel-vba

Image of the SpreadSheet我正在为我的工作创建一个Excel跟踪器,用于确定某人是否在升级到下一级别的辅助和主要区域内。我一开始只使用Excel,但这太有限了所以我决定尝试以前从未使用过的VBA。我目前有一个脚本,可以读取当前个人的排名,然后告诉我他们从排名日期到他们在主要或次要区域的日期。

我只能针对特定单元格执行此操作,我必须手动输入其自动促销日期的日期。有没有办法在整个工作表上应用相同的代码,而无需手动更改单元格。因此,如果B2包含' SPC'然后F2将有直到该行中的个人所拥有的日子,直到他们在主要区域中为SGT'如果B3例如包含PFC'等级。然后F3将显示个人在主要区域中的日期,以便SPC'等等。

Function Formula()
Workbook.Sheets("Sheet1").Range("F2").Formula = "=DATEDIF(""2/24/2017"",Today(),""d"")"
End Function

对于Macros来说就是这样的。我没有上班,所以我无法确定。

表单的代码就像

Sub Workbook_Change(ByVal Target As Range)
macroName As String
If macroName = "PFC" Then
Application.Run Formula()
ElseIf macroName = "SPC" Then
Application.Run Formula2()
EndIf
End Sub

我已经忘记了其他的东西,但它只适用于第2行,我想相应地应用于每一行。 B3& F3,B4& F4等我认为我可以自行解决的其他事情会根据排名日期自动调整主要区域结尾,而不是手动调整。

1 个答案:

答案 0 :(得分:1)

根据您展示的代码,将LIBRARY ieee; USE ieee.std_logic_1164.all; use ieee.numeric_std.all; LIBRARY work; --Add in do-file --set StdArithNoWarnings 1 ---run 0 ns; ---set StdArithNoWarnings 0 ---run 20 ms; ENTITY vhdl2_uppgift_1_extra_vhd_tst IS END vhdl2_uppgift_1_extra_vhd_tst; ARCHITECTURE vhdl2_uppgift_1_extra_arch OF vhdl2_uppgift_1_extra_vhd_tst IS -- constants constant sys_clk_period: TIME:=20 ns; -- signals SIGNAL CLOCK_50 : STD_LOGIC; SIGNAL KEY : STD_LOGIC_VECTOR(2 DOWNTO 0); SIGNAL reset_n : STD_LOGIC; SIGNAL VGA_B : STD_LOGIC_VECTOR(7 DOWNTO 0); SIGNAL VGA_BLANK_N : STD_LOGIC; SIGNAL VGA_CLK : STD_LOGIC; SIGNAL VGA_G : STD_LOGIC_VECTOR(7 DOWNTO 0); SIGNAL VGA_HS : STD_LOGIC; SIGNAL VGA_R : STD_LOGIC_VECTOR(7 DOWNTO 0); SIGNAL VGA_VS : STD_LOGIC; SIGNAL x : STD_LOGIC_VECTOR(9 DOWNTO 0); SIGNAL y : STD_LOGIC_VECTOR(9 DOWNTO 0); COMPONENT vhdl2_uppgift_1_extra PORT ( CLOCK_50 : IN STD_LOGIC; KEY : IN STD_LOGIC_VECTOR(2 DOWNTO 0); reset_n : IN STD_LOGIC; VGA_B : BUFFER STD_LOGIC_VECTOR(7 DOWNTO 0); VGA_BLANK_N : BUFFER STD_LOGIC; VGA_CLK : BUFFER STD_LOGIC; VGA_G : BUFFER STD_LOGIC_VECTOR(7 DOWNTO 0); VGA_HS : BUFFER STD_LOGIC; VGA_R : BUFFER STD_LOGIC_VECTOR(7 DOWNTO 0); VGA_VS : BUFFER STD_LOGIC; x : BUFFER STD_LOGIC_VECTOR(9 DOWNTO 0); y : BUFFER STD_LOGIC_VECTOR(9 DOWNTO 0) ); END COMPONENT; BEGIN i1 : vhdl2_uppgift_1_extra PORT MAP ( -- list connections between master ports and signals CLOCK_50 => CLOCK_50, KEY => KEY, reset_n => reset_n, VGA_B => VGA_B, VGA_BLANK_N => VGA_BLANK_N, VGA_CLK => VGA_CLK, VGA_G => VGA_G, VGA_HS => VGA_HS, VGA_R => VGA_R, VGA_VS => VGA_VS, x => x, y => y ); clock : PROCESS -- variable declarations BEGIN CLOCK_50 <= '0'; wait for sys_clk_period/2; CLOCK_50 <= '1'; wait for sys_clk_period/2; END PROCESS clock; ----------------------- reset_n <= '0', '1' after 10*sys_clk_period; ----------------------- always : PROCESS BEGIN -- code executes for every event on sensitivity list KEY <= "110"; wait for 20 ms; WAIT; END PROCESS always; ----------------------- compare: PROCESS(x,y) VARIABLE VGA_HS_error : BOOLEAN := FALSE; VARIABLE VGA_VS_error : BOOLEAN := FALSE; VARIABLE VGA_BLANK_error : BOOLEAN := FALSE; VARIABLE VGA_CLK_PHASE_error : BOOLEAN := FALSE; VARIABLE VGA_CLK_COUNTER : INTEGER :=0; VARIABLE CLOCK_50_COUNTER : INTEGER :=0; VARIABLE VGA_CLK_FREQUENCY_ERROR : BOOLEAN := FALSE; BEGIN --VGA_HS if TO_INTEGER(unsigned(x)) >= 659 AND TO_INTEGER(unsigned(x)) < 756 then if VGA_HS /= '0' then VGA_HS_error:= true; end if; ASSERT VGA_HS = '0' REPORT "FAIL on VGA_HS" SEVERITY ERROR; -- eller stanna simulatorn: SEVERITY FAILURE; -- eller REPORT "FAIL on VGA_HS signal on horizontal position " & integer'image(x); end if; --VGA_VS if TO_INTEGER(unsigned(y)) = 493 then if VGA_VS = '1' then VGA_VS_error:= true; end if; ASSERT VGA_VS = '0' REPORT "FAIL on VGA_VS" SEVERITY ERROR; -- eller stanna simulatorn: SEVERITY FAILURE; end if; --VGA_BLANK_N if TO_INTEGER(unsigned(x)) < 640 and TO_INTEGER(unsigned(y)) < 480 then if VGA_BLANK_N /= '1' then VGA_BLANK_error:= true; end if; ASSERT VGA_BLANK_N = '1' REPORT "VGA_BLANK_N" SEVERITY ERROR; -- eller stanna simulatorn: SEVERITY FAILURE; end if; --VGA_CLK if rising_edge(VGA_CLK)AND NOT rising_edge(CLOCK_50) then VGA_CLK_PHASE_error :=true; ASSERT rising_edge(CLOCK_50) REPORT "FAIL on VGA_CLK" SEVERITY ERROR; -- eller stanna simulatorn: SEVERITY FAILURE; end if; END PROCESS compare; vga_clk_test:process VARIABLE TIME_VGA_CLK_RISING : TIME; VARIABLE TIME_VGA_CLK_FALLING : TIME; VARIABLE TIME_DIFF : TIME; begin WAIT UNTIL rising_edge(VGA_CLK); TIME_VGA_CLK_RISING := now; WAIT UNTIL falling_edge(VGA_CLK); TIME_VGA_CLK_FALLING := now; TIME_DIFF := TIME_VGA_CLK_FALLING - TIME_VGA_CLK_RISING; REPORT "TEST 11: Half-periodtime : " & time'image(TIME_DIFF); IF TIME_DIFF = 20000 ps THEN REPORT "TEST 11: OK VGA_CLK "; ELSE REPORT "TEST 11: ERROR VGA_CLK"; END IF; wait; end process vga_clk_test; END vhdl2_uppgift_1_extra_arch; 中的代码添加到Formula事件本身会更容易,例如。

Worksheet_Change

这确实假设更改的单元格所在的工作表是Sheets(&#34; Sheet1&#34;)。

请注意,在更改工作表之前已禁用Application.EnableEvents。这将阻止Excel进入无限循环。

或者,您可以将更改的单元格作为参数传递给Sub Worksheet_Change(ByVal Target As Range) Dim macroName As String macroName = "something" If macroName = "PFC" Then Application.EnableEvents = False Cells(Target.Row, "F").Formula = "=DATEDIF(""2/24/2017"",Today(),""d"")" Application.EnableEvents = True ElseIf macroName = "SPC" Then Application.Run Formula2() EndIf End Sub

Formula

或者另一种方法是将更改后的单元格的行号作为参数传递给Function Formula(c As Range) Workbook.Sheets("Sheet1").Cells(c.Row, "F").Formula = "=DATEDIF(""2/24/2017"",Today(),""d"")" End Function Sub Worksheet_Change(ByVal Target As Range) Dim macroName As String macroName = "something" If macroName = "PFC" Then Application.EnableEvents = False Formula Target Application.EnableEvents = True ElseIf macroName = "SPC" Then Formula2 EndIf End Sub

Formula

为了计算正确的日期(并根据我的第一种编码方法),您可以执行以下操作:

Function Formula(r As Long)
    Workbook.Sheets("Sheet1").Cells(r, "F").Formula = "=DATEDIF(""2/24/2017"",Today(),""d"")"
End Function

Sub Worksheet_Change(ByVal Target As Range)
    Dim macroName As String
    macroName = "something"
    If macroName = "PFC" Then
        Application.EnableEvents = False
        Formula Target.Row
        Application.EnableEvents = True
    ElseIf macroName = "SPC" Then
        Formula2
    EndIf
End Sub