vba代码怎么写

1.excel vba代码要怎么写用如何代码可实现,假设原数据在A、B列
Sub main()
Set dic = CreateObject("scripting.dictionary")
arr = Range("A1").CurrentRegion
For i = 1 To UBound(arr)
If dic.exists(arr(i, 2)) = False Then
dic(arr(i, 2)) = arr(i, 1)
【vba代码怎么写】Else
If dic(arr(i, 2)) dic.Remove arr(i, 2)
dic(arr(i, 2)) = arr(i, 1)
End If
End If
Next i
[E1].Resize(dic.Count, 1) = Application.Transpose(dic.keys)
[D1].Resize(dic.Count, 1) = Application.Transpose(dic.items)
End Sub
详细步骤如下:
1. 原数据
2. Alt+F11,选择sheet1,复制上述代码
3. 关闭返回工作表,alt+F8,掉出宏界面
4. 点击执行,即得到想要的结果:
2.VBA代码在哪里写 怎么用打开EXCEL-"工具"-"宏"-"Visual Basic 编辑器"
选择相应的表文件就能输入代码.
但提议:
如有控件按钮的,编辑状态下直接在按钮击就能打击输入代码的窗口了.
如是表格单元的公式运算,先试用"工具"-"宏"-"录制新宏" 将几个要用的操作录制起来,再按上面的方式打开VBS编辑器去看看代码怎写的,学习一下标准代码的规范.
(新手用点笨办法没坏处,太高级经常有遇想不到的错误发生.)
3.excel vba 这段代码怎么写好了写完了 。
7a686964616fe58685e5aeb931333236373264注,原数列必须要A列,代码加入后,在excel中双击就可以了 。Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)i = 1newi = 1newj = 1Do While (IsNumeric(ActiveSheet.Cells(i, 1))) And (CStr(ActiveSheet.Cells(i, 1)) <> "")If CInt((i - 1) / 9) = (i - 1) / 9 Then ActiveSheet.Cells(newi, newj) = ActiveSheet.Cells(i, 1) newi = newi + 1Else ActiveSheet.Cells(newi, newj) = ActiveSheet.Cells(i, 1) If newj < 4 Then newj = newj + 1 Else newj = 1 newi = newi + 1 End IfEnd Ifi = i + 1LoopFor x = newi To i ActiveSheet.Cells(x, 1) = ""NextEnd Sub 。
4.怎么写一段excel vba 输入代码1. 从Excel工具栏中插入一个VBA的控件-编辑框 。
2. 双击这个编辑框,进入VBA编程窗口 。
3. 删除原来的所有内容,输入以下内容:
Private Sub TextBox1_LostFocus()
Range("A1").Value = http://www.xuexi88.com/zhishi/TextBox1.Text
End Sub
4. 关闭编程窗口,回到Excel界面 。
5. 在编辑框中输入一个字串 。
6. 用鼠标点击编辑框之外的其它地方,看看所输入的字串是不是出现在A1
5.VBA代码中怎么写连加可以借用EXCEL的求和公式:
如:
[c1] = Application.WorksheetFunction.Sum(Range("a1:b5"))
可以对单元格区域a1:b5求和,并把结果写在C1单元格
如果直接写的话,别忘了在单元格两边加上中括号:
如:
A=[A1]+[A3]+[A5]
可以把A1,A3,A5的和赋值给变量A
补充回答:
下面代码可以把A1到A100的和赋值给变量A
dim i ,a as long
a=0
for i=1 to 100
a=a+cells(i,1)
next i
对于VBA,一般尽量避免用循环的方法:它的效率是比较低的!
楼主什么意思?这有什么区别么?CELLS函数行和列用的都是数字,直接用就行了
最简单的方法:用循环嵌套
dim i,j,a as long
a=0
for j=1 to 10
for i=1 to 100
a=a+cells(i,j)
next i
next j
连续区域也可以用for each循环:
Dim cell1, a As Long
a = 0
For Each cell1 In Range("a1:c100")
a = a + cell1
Next
6.求VBA代码写法Sub abc()
Dim C As Range, rng1 As Range, rng2 As Range, Arr
With Workbooks("A.xls")
Arr = .Sheets("sheet1").Range("A1:A3")
With .Sheets("B")
Set rng1 = Application.Intersect(.Range("B:B"), .UsedRange)
End With
For Each C In rng1
tmp = C.Value