将Excel表宏处理为另一种格式。删除重复项并重新格式化

时间:2019-07-03 11:26:20

标签: excel vba string manipulate

我在VBA旅程中大约有两个月的时间,遇到了一个无法在线解决的问题。我在将Excel表转换为由我编程的另一个宏创建的另一种格式时遇到问题。我有一张表格,在不同的行上有国家和一个数字的名称,在几行上列出了。我希望它是动态的,因为此表每天都会更新

我在下面写了我想要它的外观。我的想法是将国家/地区编码为数字,然后删除国家/地区中的重复项。

我试图创建一个循环,但我想可能必须为每个国家/地区创建一个范围。

Sub ManipulateTable()
Dim Country as String
Dim USA as Range
Dim EU as Range
Dim India as Range

Const StartRow As Byte = 7
Dim LastRow as Long
LastRow = Range("A" & StartRow.(End(xlDown).Row

For i StartRow to LastRow 
Country = Range("A" & i).Value

If Country = "USA" Then Range("C" & i).value = 1
If Country = "EU" Then Range("C" & i).value = 2
If Country = "India" Then Range("C" & i).value = 3
Next i
' This to remove duplicates from column a
Range("A7:A30").RemoveDuplicates Columns:=Array(1). Header:= xlYes
' I thinking that I need to create a loop here 
' But I dont know where to start
For i StartRow to LastRow
Countryindex =  Range("C").Value
If Countryindex = 1 Then put under USA
If Countryindex = 2 Then put under EU

我的表格看起来像这样,带有单独的列

    "A" "B"
     Data
1    USA Sales
2    USA Employment Figures
3    USA Tax
4    EU Sales
5    EU Employment Figures
6    India Sales
7    India Expenses 
8    India Employment Figures

我想要一张看起来像这样的桌子

 "A" 
 Data
1    USA: (With some color)
2    Sales
3    Employment
4    Tax
5    EU: (With some color)  
6    Sales
7    Employment
8    India: (With some color)
9    Sales
10   Expenses
11   Employment

高度重视所有帮助。

Country Data Time

Country:
Data Time

1 个答案:

答案 0 :(得分:0)

在运行此操作之前保存副本,它将覆盖您的数据。

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-9381f16ca1ad> in <module>()
      9 
     10 net = mv_oberrhein()
---> 11 simple_plotly(net, on_map=True, projection='epsg:31467')

~/miniconda3/lib/python3.6/site-packages/pandapower/plotting/plotly/simple_plotly.py in simple_plotly(net, respect_switches, use_line_geodata, on_map, projection, map_style, figsize, aspectratio, line_width, bus_size, ext_grid_size, bus_color, line_color, trafo_color, ext_grid_color)
    116         **ext_grid_color** (String, 'yellow') - External Grid Color. Init is yellow
    117     """
--> 118     version_check()
    119     # create geocoord if none are available
    120     if 'line_geodata' not in net:

~/miniconda3/lib/python3.6/site-packages/pandapower/plotting/plotly/traces.py in version_check()
     33 
     34 def version_check():
---> 35     if version.parse(plotly_version) < version.parse("3.1.1"):
     36         raise UserWarning("Your plotly version {} is no longer supported.\r\n"
     37                           "Please upgrade your python-plotly installation, "

NameError: name 'plotly_version' is not defined